user492922
user492922

Reputation: 935

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

Upvotes: 1

Views: 710

Answers (1)

Snowbear
Snowbear

Reputation: 17274

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?

Upvotes: 2

Related Questions