Linq project and Datatable

  • Thread starter Thread starter mtczx232
  • Start date Start date
Certainly and it is called DLinq (just check the link (minus \samples part)
that David gave you.
 
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?
 
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

}

}
 
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?

Yes that's possible. Every IEnumerable<T> is queryable through Linq.
This is simply traversing the list and returning a new IEnumerable<T>
with the elements of type T matching the query.

Frans


--
 
Frans Bouma said:
Yes that's possible. Every IEnumerable<T> is queryable through Linq.
This is simply traversing the list and returning a new IEnumerable<T>
with the elements of type T matching the query.

The problem is that DataRowCollection doesn't implement IEnumerable<T> at
this time.... :-)
 
Miha said:
The problem is that DataRowCollection doesn't implement
IEnumerable<T> at this time.... :-)

No, but IEnumerable of course also works if I'm not mistaken (as
IEnumerable<T> implements IEnumerable)

FB

--
 
Miha Markic thanks.

I need more info about this, if you like:

i'm not familiar with <T> syntax (this is the same as Templet in C++?)
this code can convert to VB?

eventually my goal to be able do grouping like:
"select count([id]),city from customers group by city"
it is possible todo this on Datatable with Linq?

thanks
 
No, but IEnumerable of course also works if I'm not mistaken (as
IEnumerable<T> implements IEnumerable)

AFAIK it would work if you cast IEnumerable<T> to IEnumerable but not the
opposite direction.
 
Back
Top