I have a data table and I want to select all distinct names from the result. I wrote following linq query for it.
var distinctRows = (from DataRow myDataRow in myDataTable.Rows
select new { col1 = myDataRow ["Name"]}).Distinct();
Now how can I iterate through distinctRows? Seems like I cannot do foreach(DataRow Row in distinctRows), It gives me “Cannot convert type ‘AnonymousType#1’ to ‘System.Data.DataRow'” error
Since you’re only selecting one field, you don’t need an anonymous type here. Just select the names and then iterate over the distinct ones. To wit:
Note that the error makes it very clear what the problem is here. You are trying to convert an instance of an anonymous type to an instance of
DataRowand that is impossible. Without changing your code, you could iterate this asBut I would change this as per the above as you don’t need the anonymous type and your variable names and field names are poor.