Outer join with Linq causing “GroupJoin” error

試著忘記壹切 提交于 2019-12-12 04:25:14

问题


The following join projection is throwing the error, "The 'GroupJoin' operation must be followed by a 'SelectMany' operation where the collection selector is invoking the 'DefaultIfEmpty' method." I've run over a few permutations of changes, but haven't been able to figure it out. Thoughts?

EDIT: It's looking more and more like this may be a Dynamics CRM issue. The Xrm models I'm accessing for data have been generated by the CRM SDK (CRM 2011).

var q =
   left
     .GroupJoin(right, 
                c => c.Id, 
                cl => cl.c.Id, 
                (c, cs) => new { c, cs })
     .Where(x=>x.c.Name.Contains("some text"))
     .SelectMany(x => x.cs.DefaultIfEmpty(), (x, csubl) =>
        new
        {
            CompanyName = x.c.Name
        });

回答1:


i think in query syntax it will be more beautiful

var q = from l in left
        join r in right on l.Id equals r.c.Id into groupped
        from g in groupped.DefaultIfEmpty()
        where l.Name.Contains("some text")
        select new { 
            CompanyName = l.Name; 
        }

UPDATE
in samples from msdn have sample with left join, so you can try my code above, or move Where before GroupJoin or after SelectMany



来源:https://stackoverflow.com/questions/20336653/outer-join-with-linq-causing-groupjoin-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!