LINQ Query on Datatable

Posted by Preddy308 under Others on 6/23/2011 | Points: 10 | Views : 9790 | Status : [Member] | Replies : 5
Hi,

I am unable to LINQ Query on a datatable.
It is throwing me the error : Cannot convert lambda expression to type 'string' because it is not a delegate type.
var query = from temp in dt
select name;

error is throwing at select statement.

Can anybody please help with this Issue.

Thanks.




Responses

Posted by: SheoNarayan on: 6/23/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
You can't do directly the way you do for collections, you will have to change the DataTable to enumerable and use .Field<"fieldName">.

Something like http://stackoverflow.com/questions/10855/linq-query-on-a-datatable.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/23/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
http://unboxedsolutions.com/sean/archive/2009/06/06/15961.aspx

Regards,
Susan

Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/23/2011 [Member] Starter | Points: 25

Up
0
Down
You can cast them to Enumerable of DataRow, then apply LINQ on that.
if dt is the name of your data table you change it in following ways then you can apply linq on it.
dt.AsEnumerable()

OR
dt.Rows.Cast<DataRow>()




Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/24/2011 [Member] Starter | Points: 25

Up
0
Down
change you query as

dataclassesdatacontext dt=new dataclassesdatacontext();

var query = from temp in dt.tablename select name;




Thanks&Regards
LakshmiNarayana Nalluri.

Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Preddy308 on: 6/24/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks to all for posting back...
dt.AsEnumerable() ... and field attribute worked in where clause..

Preddy308, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response