.net Linq多表組合查詢結果集


背景:

主流程表中包含員工ID及流程類型ID,在頁面查看流程是需要顯示員工姓名及流程名稱

操作:

 //先進行分頁處理原始數據
            var result = from p in _dbContext.Set<BeWorkflowContent>()
                         select p;
            if (searchWhere != null)
                result = result.Where(searchWhere);
            if (order != null)
                result = result.OrderByDescending(order);
            rowCount = result.Count();
            
            //多表查詢組裝最后的數據模型
            var query = from a in result
                        join b in _dbContext.BeUser on a.UserId equals b.UserId
                        join c in _dbContext.BeWorkflow on a.WorkflowId equals c.WorkflowId
                        select new WorkFlowContentV()
                        {
                            CustomerName = a.CustomerName,
                            WorkflowContentId = a.WorkflowContentId,
                            CarType = a.CarType,
                            CarStyle = a.CarStyle,
                            CarLicense = a.CarLicense,
                            UserName = b.TrueName,
                            WorkflowName = c.WorkflowName
                        };
            return query.ToList();
View Code

 

PS:select new之后一定要加對應的類名,不然返回后就是對象轉換時會有問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM