Reputation: 1689
I have a class R
with some properties.
Similarly I have a DataTable
dt
with columns exactly same as the properties of the class R
I am trying to create 2 different DataTables
from the above said DataTable
dt
based on a condition on one of the columns of the dt
.
And I want to convert these 2 Datatables
into a List
of R
objects.
This is how I am trying with now progress:
List<R> Sheet1 = dt.AsEnumerable()
.Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) <= Convert.ToDecimal(80));
List<R> Sheet2 = dt.AsEnumerable()
.Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) > Convert.ToDecimal(80));
But it throws an error:
Cannot convert type 'System.Data.EnumerableRowCollection' to generic.list
I tried doing all kinds of casting but with no use:
List<R> Sheet1 = (List<R>)dt.AsEnumerable()
.Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) <= Convert.ToDecimal(80));
List<R> Sheet2 = (List<R>)dt.AsEnumerable()
.Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) > Convert.ToDecimal(80));
List<R> Sheet1 = (List<R>)dt.AsEnumerable()
.Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) <= Convert.ToDecimal(80))
.ToList();
List<R> Sheet2 = (List<R>)dt.AsEnumerable()
.Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) > Convert.ToDecimal(80))
.ToList();
Upvotes: 2
Views: 4530
Reputation: 39976
Your Where
statement is returning EnumerableRowCollection<DataRow>
. You need to select a List
of R
from your query to make this works:
List<R> Sheet1 = dt.AsEnumerable().
Where(row => Convert.ToDecimal(row["SomeDecimalColumn"]) <= Convert.ToDecimal(80))
.Select(c => new R
{ SomeDecimalColumn = Convert.ToDecimal(c["SomeDecimalColumn"]) /*other propertie*/ })
.ToList();
Upvotes: 2