I need to run a LINQ query against a DataTable to get distinct values using multiple columns. If I know the columns I need to select beforehand, I can use the answer from David Hoarser at Select distinct rows from datatable in Linq:
var distinctValues = dsValues.AsEnumerable()
.Select(row => new {
attribute1_name = row.Field<string>("attribute1_name"),
attribute2_name = row.Field<string>("attribute2_name")
})
.Distinct();
However, in my case, I have the column names I need to select in a list of objects:
public class PC{
public string Name{get;set;}
public string NumFormat{get;set}
}
List<PC>cols=new List<PC>();
cols.AddRange(new PC[]{new PC{Name="FullName",NumFormat=""},
{new PC{Name="Salary",NumFormat="$ #,##0"}});
I could use David's example from above to build the select statement as:
var distinctValues = dsValues.AsEnumerable()
.Select(row => new {
attribute1_name = row.Field<string>(cols[0].Name),
attribute2_name = row.Field<string>(cols[1].Name)
})
.Distinct();
But this won't work since I don't know how many columns I need to select Distinct over since the cols list can vary. How can I loop over the list of columns to build my Select statement to ultimately get my Distinct values?