为什么EnumerableRowCollection< DataRow> .Select()不能这样编译? [英] Why doesn't EnumerableRowCollection<DataRow>.Select() compile like this?

查看:523
本文介绍了为什么EnumerableRowCollection< DataRow> .Select()不能这样编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

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,但是显然我错了.我想念什么?

Looking at the two overloads of the .Select() method, I thought the latter two should both return EnumerableRowCollection, but apparently I am wrong. What am I missing?

推荐答案

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

The problem is you're combining two ways of performing a linq query (query syntax and calling the linq extension methods directly). The line from x in table.AsEnumerable() is not a valid query since it require at least a select .... This should work:

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

这篇关于为什么EnumerableRowCollection&lt; DataRow&gt; .Select()不能这样编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆