我有以下表格:
Table1
{
Code //string
Desc //string
}
Table2
{
Code //string
Value //decimal?
}
Run Code Online (Sandbox Code Playgroud)
我需要左连接表,并且对于每个缺少的 Table2 代码/值,我想显示 Code = Table1.Code、Desc = Table1.Desc 和 Value = null 或空白。
我尝试了以下 lambda 表达式:
var result = Table1.GroupJoin(
Table2,
x => x.Code,
y => y.Code,
(x, y) => g
.Select(c => new { x.Code, x.Desc, Value = y.Value })
.DefaultIfEmpty(new { x.Code, x.Desc, Value = null }))
.SelectMany(g => g);
Run Code Online (Sandbox Code Playgroud)
并得到这些错误:
无法从用法中推断出方法 'System.Linq.Enumerable.DefaultIfEmpty(System.Collections.Generic.IEnumerable, TSource)' 的类型参数。尝试显式指定类型参数。
无法分配给匿名类型属性
所以,我改变了 ...DefaultIfEmpty... Value = 0 }...
并得到这些错误: …