gpt4 book ai didi

c# - 为什么 EnumerableRowCollection.Select() 不能这样编译?

转载 作者:太空狗 更新时间:2023-10-30 01:27:35 27 4
gpt4 key购买 nike

这个有效:

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

关于c# - 为什么 EnumerableRowCollection<DataRow>.Select() 不能这样编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2560776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com