I see a number of examples to group by multiple columns. I get that.
I have a DataTable and I want to get the distinct rows based on specific fields.
For example this works: (there are more than these two fields in the table) but I want those that are distinct on these two, returning all the fields)
var rtable = myDataTable.AsEnumerable()
.GroupBy(a => new { FirstId = a["FirstId"], SecondId = a["SecondId"] })
.Select(a => a.First()).CopyToDataTable();
I have a list of columns and want to use my list of columns as those to group by. I also have these field name strings in a list.
Simple example:
var groupMap = new List<MyMap>{
new MyMap(){FieldName = "FirstId", SomeOtherThing= "Hi", IsUnique = true},
new MyMap(){FieldName = "SecondId", SomeOtherThing= "Cats", IsUnique = true },
new MyMap(){FieldName = "FirstName", SomeOtherThing= "Dogs", IsUnique = false},
};
I want to use the groupMap
property FieldName
where IsUnique=true
to create my columns to use to GroupBy
. Since I have this list, how can I use it to create the GroupBy
with this criteria? Its sort of trivial to retype stuff from the list but I have several of these and want a more generic solution based on the properties of the MyMap
class lists.