The linq can query Datatable? have some example?

Re: Linq project and Datatable by David

David
Sun Jan 15 12:02:06 CST 2006

Check this out: http://msdn.microsoft.com/vcsharp/future/linqsamples/

David Barkol
www.neudesic.com


Re: Linq project and Datatable by Miha

Miha
Sun Jan 15 13:18:18 CST 2006

Certainly and it is called DLinq (just check the link (minus \samples part)
that David gave you.

--
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:1137329662.827361.21180@o13g2000cwo.googlegroups.com...
> The linq can query Datatable? have some example?
>



Re: Linq project and Datatable by mtczx232

mtczx232
Sun Jan 15 15:18:52 CST 2006

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?


Re: Linq project and Datatable by Miha

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



Re: Linq project and Datatable by Miha

Miha
Sun Jan 15 16:45:00 CST 2006

And you might also read my blog post on the subject:
http://cs.rthand.com/blogs/blog_with_righthand/archive/2006/01/15/284.aspx

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



Re: Linq project and Datatable by Frans

Frans
Mon Jan 16 03:33:57 CST 2006

mtczx232@yahoo.com wrote:

> 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


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Re: Linq project and Datatable by Miha

Miha
Mon Jan 16 04:07:57 CST 2006


"Frans Bouma [C# MVP]" <perseus.usenetNOSPAM@xs4all.nl> wrote in message
news:xn0eharq13fs92000@news.microsoft.com...
> mtczx232@yahoo.com wrote:
>
>> 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.

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

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/



Re: Linq project and Datatable by Frans

Frans
Mon Jan 16 05:31:48 CST 2006

Miha Markic [MVP C#] wrote:

>
> "Frans Bouma [C# MVP]" <perseus.usenetNOSPAM@xs4all.nl> wrote in
> message news:xn0eharq13fs92000@news.microsoft.com...
> > mtczx232@yahoo.com wrote:
> >
> >> 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.
>
> 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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Re: Linq project and Datatable by mtczx232

mtczx232
Mon Jan 16 06:02:49 CST 2006

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


Re: Linq project and Datatable by Miha

Miha
Mon Jan 16 11:35:06 CST 2006



> 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.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/