Miha
Sun Jan 15 16:24:00 CST 2006
Hi there,
Ah, I see - you want to roll Linq over DataTable class (.Rows probably).
AFAIK it isn't possible at the moment without some coding - you would have
to create a Rows property that returns IEnumerable<DataRow>.
Here you go with an example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
using System.Data;
using System.Collections;
namespace LINQConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(int));
table.Rows.Add(new object[]{1});
table.Rows.Add(new object[]{2});
table.Rows.Add(new object[]{3});
RhDataRowCollection rows = new RhDataRowCollection(table.Rows);
IEnumerable<DataRow> selectedRows = from r in rows
where (int)r["Id"] == 2
select r;
foreach (DataRow row in selectedRows)
Console.WriteLine(row["Id"]);
Console.WriteLine("Finished");
Console.ReadLine();
}
}
internal class RhDataRowCollection: IEnumerable<DataRow>, IEnumerable
{
DataRowCollection rows;
internal RhDataRowCollection(DataRowCollection rows)
{
this.rows = rows;
}
#region IEnumerable<DataRow> Members
IEnumerator<DataRow> IEnumerable<DataRow>.GetEnumerator()
{
foreach (DataRow row in rows)
yield return row;
}
IEnumerator IEnumerable.GetEnumerator()
{
IEnumerable<DataRow> ie = this;
return ie.GetEnumerator();
}
#endregion
}
}
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog:
http://cs.rthand.com/blogs/blog_with_righthand/
<mtczx232@yahoo.com> wrote in message
news:1137359932.501638.293950@g49g2000cwa.googlegroups.com...
> i'm not sure that Dlinq deal with Datatable!!??
> again my q: Have a simple way to Query Datatable by Linq?
> without build new Data modeling or Link to any Database.
> just query datatable?
>