Why is EnumerableRowCollection <DataRow> .Select () not compiling?

It works:

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

but this is not so:

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

I also tried:

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

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

+3
source share
2 answers

The problem is that you are combining the two methods for executing a linq query (query syntax and direct calling of linq extension methods). The string is from x in table.AsEnumerable()not a valid query since it requires at least a select .... This should work:

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

, . :

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

Source: https://habr.com/ru/post/1739469/


All Articles