查询主体必须以LINQ Query中的select子句或group子句结束

时间:2017-09-14 06:37:42

标签: asp.net-mvc linq

model.ListManagerReviewerMapping = (from a in wallet.OM_Employee 
                                    join m in wallet.APPR_ManagerMapping 
                                    on a.AssociateId equals m.AssociateId
                                    where m.ManagerId==Context.UserId.Value **into** MM 
                                    from leftjoinresult in M.DefaultIfEmpty() 
                                    where a.CompanyId == Context.CompanyId && (a.TermStatus == "L" || a.SeparationDate > DateTime.Today) 
                                    select new ManagerAndReviewerMappingModel.ManagerAndReviewerMapping() 
                                    { 
                                        Photo = (photoUrl + "?AssociateCode=" + a.AssociateCode), 
                                        AssociateId = a.AssociateId,
                                        AssociateCode = a.AssociateCode,
                                        AssociateName = a.AssociateName
                                    }).ToList();

2 个答案:

答案 0 :(得分:1)

//Remove brackets and .ToList();
model.ListManagerReviewerMapping = from a in wallet.OM_Employee 
                                join m in wallet.APPR_ManagerMapping 
                                on a.AssociateId equals m.AssociateId
                                where m.ManagerId==Context.UserId.Value **into** MM 
                                from leftjoinresult in M.DefaultIfEmpty() 
                                where a.CompanyId == Context.CompanyId && (a.TermStatus == "L" || a.SeparationDate > DateTime.Today) 
                                select new ManagerAndReviewerMappingModel.ManagerAndReviewerMapping() 
                                { 
                                    Photo = (photoUrl + "?AssociateCode=" + a.AssociateCode), 
                                    AssociateId = a.AssociateId,
                                    AssociateCode = a.AssociateCode,
                                    AssociateName = a.AssociateName
                                };

答案 1 :(得分:1)

您需要对第一个查询使用Where扩展方法,因为查询使用DefaultIfEmpty左连接(请注意,into子句后您不能使用where由于where必须跟select一起完成查询):

model.ListManagerReviewerMapping = (from a in wallet.OM_Employee 
                                    join m in wallet.APPR_ManagerMapping.Where(x => x.ManagerId == Context.UserId.Value)
                                    on a.AssociateId equals m.AssociateId into MM 

                                    from leftjoinresult in MM.DefaultIfEmpty() 
                                    where a.CompanyId == Context.CompanyId && (a.TermStatus == "L" || a.SeparationDate > DateTime.Today) 
                                    select new ManagerAndReviewerMappingModel.ManagerAndReviewerMapping() 
                                    { 
                                        Photo = (photoUrl + "?AssociateCode=" + a.AssociateCode), 
                                        AssociateId = a.AssociateId,
                                        AssociateCode = a.AssociateCode,
                                        AssociateName = a.AssociateName
                                    }).ToList();

类似问题:

LINQ LEFT JOIN where clause not working

LINQ Left Join And Right Join