c# - 来自左外连接的分组值

标签 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
            };

最佳答案

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

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
            };

关于c# - 来自左外连接的分组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56627025/

相关文章:

c# - 尝试从性能监视器中提取 CPU 和网络使用信息

c# - 使用过滤器但缺少引用的包中的错误处理

c# - Visual Studio 可以将项目引用编译到不同的文件夹然后是主 .exe

c# - 按模型分组并选择模型

c# - 如何在可由另一个线程更改的 IEnumerable 上安全地调用 Count(Func)?

c# - Linq 左外连接 - DefaultIfEmpty 错误

c# - C# 中的麦克风到扬声器音频流

c# - 为 BitArray 生成良好的哈希码 (GetHashCode)

c# - 根据子集合总结一个属性

c# - 如何使用LINQ查找值的总和