I have the following tables:
Table1
{
Code //string
Desc //string
}
Table2
{
Code //string
Value //decimal?
}
I need to Left Join the tables and for each Table2 Code/Value missing I want to show Code = Table1.Code, Desc = Table1.Desc and Value = null or blank.
I tryied the following lambda expression:
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);
and got these errors:
The type arguments for method 'System.Linq.Enumerable.DefaultIfEmpty(System.Collections.Generic.IEnumerable, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Cannot assign to anonymous type property
So, I changed ...DefaultIfEmpty... Value = 0 }...
并得到以下错误:'System.Collections.Generic.IEnumerable'不包含'DefaultIfEmpty'的定义,最佳扩展方法重载'System.Linq.Queryable.DefaultIfEmpty(System.Linq.IQueryable,TSource)'有一些无效论点
实例参数:无法从“ System.Collections.Generic.IEnumerable”转换为“ System.Linq.IQueryable”
有解决错误的想法吗?
您只需要null
在匿名类型初始值设定项中指定值的类型:
.DefaultIfEmpty(new { x.Code, x.Desc, Value = (decimal?) null }))
使用0时,您正在创建一个单独的匿名类型,该类型的Value
属性类型为int
,而不是decimal?
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句