为什么 EnumerableRowCollection.Select() 不能像这样编译?

2024-04-03

这有效:

from x in table.AsEnumerable()
where x.Field<string>("something") == "value"
select x.Field<decimal>("decimalfield");

但是,这不会:

from x in table.AsEnumerable()
.Where(y=>y.Field<string>("something") == "value")
.Select(y=>y.Field<decimal>("decimalfield"));

我也尝试过:

from x in table.AsEnumerable()
.Where(y=>y.Field<string>("something") == "value")
.Select(y=>new { name = y.Field<decimal>("decimalfield") });

看看 .Select() 方法的两个重载,我认为后两个都应该返回 EnumerableRowCollection,但显然我错了。我缺少什么?


问题是您组合了两种执行 linq 查询的方法(查询语法和直接调用 linq 扩展方法)。线路from x in table.AsEnumerable()不是有效的查询,因为它至少需要一个select ...。这应该有效:

table.AsEnumerable() 
.Where(y=>y.Field<string>("something") == "value") 
.Select(y=>new { name = y.Field<decimal>("decimalfield") });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 EnumerableRowCollection.Select() 不能像这样编译? 的相关文章

随机推荐