LINQ Query to DataTable.DataSource
Posted
by lumberjack
on Stack Overflow
See other posts from Stack Overflow
or by lumberjack
Published on 2010-03-23T19:09:07Z
Indexed on
2010/03/23
19:13 UTC
Read the original article
Hit count: 434
I am trying to perform a LINQ query on a DataTable and show the result in another DataTable. My source DataTable looks something like this:
DataTable myDataTable = new DataTable();
myDataTable.Columns.Add("OrderID", typeof(int));
myDataTable.Columns.Add("Date", typeof(DateTime));
myDataTable.Columns.Add("UnitsPurchased", typeof(int));
The resulting DataTable looks like this when filled:
Order ID Date Units Purchased 16548 10/15/09 250 17984 11/03/09 512 20349 01/11/10 213 34872 01/15/10 175
My current LINQ query looks like this:
IEnumerable<DataRow> query = (from row in myDataTable.AsEnumerable()
where row.UnitsPurchased > 200
select new
{
row.OrderID,
row.Date,
row.UnitsPurchased
}) as IEnumerable<DataRow>;
resultDataTable.DataSource = query.CopyToDataTable<DataRow>();
Every time I run this code query is null. I can see that that the as IEnumerable<DataRow>
is the culprit, but it makes no since to me since DataTable.AsEnumerable()
returns an IEnumerable<DataRow>
. Any help would be appreciated.
© Stack Overflow or respective owner