左外部联接的分组值

时间:2019-06-17 07:57:08

标签: c# linq

我有一个简单的问题。我想使用左外部联接的分组值。 但是我得到了异常“对象引用未设置...”。 我在哪里错了,您能帮我吗?

我的代码;

var query = from a in aList
            join b in bList n a.id equals b.id into bJoin
            from bVal in bJon.DefaultIfEmpty()
            group new { a.x, a.y, a.z, bVal.x } by a.q into grp
            select new {
               q = a.q,
               ....
               x = grp.Max(max=>max.a.x)!=null ? grp.Max(max=>max.b.x) : string.Empty
            };

1 个答案:

答案 0 :(得分:0)

我解决了问题。您可以在下面找到解决方案代码;

var query = from a in aList
            join b in bList n a.id equals b.id into bJoin
            from bVal in bJon.DefaultIfEmpty()
            group new { a.x, a.y, a.z, 
               newX = (bVal == null ? string.Empty : bVal.x)
            } 
            by a.q into grp
            select new {
               q = a.q,
               ....
               x = grp.Max(max=>max.a.x)!=null ? grp.Max(max=>max.newX) : string.Empty
            };