4

これは機能します:

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()メソッドの2つのオーバーロードを見て、後者の2つは両方ともEnumerableRowCollectionを返す必要があると思いましたが、明らかに私は間違っています。私は何が欠けていますか?

4

2 に答える 2

4

問題は、linq クエリを実行する 2 つの方法 (クエリ構文と linq 拡張メソッドの直接呼び出し) を組み合わせていることです。from x in table.AsEnumerable()少なくともselect .... _ これはうまくいくはずです:

table.AsEnumerable() 
.Where(y=>y.Field<string>("something") == "value") 
.Select(y=>new { name = y.Field<decimal>("decimalfield") });
于 2010-04-01T13:57:45.977 に答える
0

多分問題は別の場所にあります。これはうまくコンパイルされます:

using System.Data;

class Program
{
    static void Main(string[] args)
    {
        var dt = new DataTable();

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

        var res2 = dt.AsEnumerable()
            .Where(y => y.Field<string>("something") == "value")
            .Select(y => y.Field<decimal>("decimalfield"));
    }
}
于 2010-04-01T13:54:00.297 に答える