在现有的左连接linq查询中使用IN查询中的另一个列表项

时间:2015-06-23 05:02:06

标签: c# linq dynamic

dynamic memberEomMapping;
                using (var context = new MIHR.Business.EF.SA_MIHR_DEVEntities())
                {
                    memberEomMapping = (from c in context.MemberEomMapping
                                        join
                                            b in context.EntityOrgModule on c.EomId equals b.Id 
                                        where c.MemberPersonId == PersonId &&  b.EOMTYPEID == 116001
                                        select new
                                        {
                                            c.EomId,
                                            b.Name
                                        }).ToList();

                }

我有这个列表,它返回了一个EomId和Name的列表,现在在这个查询旁边我有另一个查询,其中我想获取此列表中的项目列表(简而言之,使用此查询结果,如查询另一个列表)

这是另一个列表的代码

dynamic entityOrgModule;
                using (var context = new MIHR.Business.EF.SA_MIHR_DEVEntities())
                {

                    entityOrgModule = (from c in context.EntityOrgModule
                                       join p in context.MemberEomMapping on c.Id equals p.EomId into gj
                                       from x in gj.DefaultIfEmpty()
                                       where c.EntityType == UserEntityTypeId && c.EntityId == UserEntityId && c.Rowstatus == 1 && c.EOMTYPEID == 116002 && (c.ParentId==2 || c.ParentId==3)
                                       select new
                                       {
                                           c.Id,
                                           c.Name,
                                           c.ParentId,
                                           isSelected = (x == null ? false : true)
                                       }).ToList();

                }

所以在上面的查询而不是(c.ParentId==2 || c.ParentId==3)中,应该从memberEomMapping列表中使用此条件。

我尝试过这样的事情

dynamic entityOrgModule;
                using (var context = new MIHR.Business.EF.SA_MIHR_DEVEntities())
                {

                    entityOrgModule = (from c in context.EntityOrgModule
                                       join p in context.MemberEomMapping on c.Id equals p.EomId into gj
                                       from x in gj.DefaultIfEmpty()
                                       where c.EntityType == UserEntityTypeId && c.EntityId == UserEntityId && c.Rowstatus == 1 && c.EOMTYPEID == 116002 && memberEomMapping.Contains(c.ParentId) 
                                       select new
                                       {
                                           c.Id,
                                           c.Name,
                                           c.ParentId,
                                           isSelected = (x == null ? false : true)
                                       }).ToList();

                }

但这给了我另一个编译时错误

表达式树可能不包含动态操作

1 个答案:

答案 0 :(得分:0)

试试这个,使用.Any()

修改:

替换dynamic memberEomMapping;

var memberEomMapping = new[]{new { EomId = 0, Name = "" }}.ToList();

List<int> mylist = memberEomMapping.Select(x => x.EomId).ToList();

然后

dynamic entityOrgModule;
            using (var context = new MIHR.Business.EF.SA_MIHR_DEVEntities())
            {

                entityOrgModule = (from c in context.EntityOrgModule
                                   join p in context.MemberEomMapping on c.Id equals p.EomId into gj
                                   from x in gj.DefaultIfEmpty()
                                   where c.EntityType == UserEntityTypeId && c.EntityId == UserEntityId && c.Rowstatus == 1 && c.EOMTYPEID == 116002 && mylist.Any(y => y == c.ParentId) 
                                   select new
                                   {
                                       c.Id,
                                       c.Name,
                                       c.ParentId,
                                       isSelected = (x == null ? false : true)
                                   }).ToList();

            }