Returning the correct type in C# linq query over DataRows
I can't seem to get the following working
var results = (from DataRow row in myDataView.Table.Rows
开发者_如何学C group row by row["part"] into x
select new {x}).Distinct();
foreach(var x in results)
{
doSomething(x["part"]);
doSomethingElse(x["field2"]);
}
The problem seems to be that results contains elements of "AnonymousType" which I guess is an artifact of "group". How can I make the above code work as I expect?
new {x}
this will create an anonimous type with property 'x' where x object will be stored. This is short syntax for:
new {x = x} // first x is property name
in your case you should simply select x:
var results = (from DataRow row in myDataView.Table.Rows
group row by row["part"] into x
select x).Distinct();
Also Distinct() after Group looks strange. What did you mean?
精彩评论