c# - Using Linq to remove duplicate by column values based on a second column -
i have datatable 2 columns "dt" , "asof", both dates. there rows has duplicate values of dt, find doing:
var duplicategroups = dt.asenumerable() .groupby(row => row.field<sqldatetime>("dt")) .where(g => g.count() > 1);
but there column, "asof" want rid of 1 of duplicate dts based on "asof" value, ever asof date newest.
i can think of loop able creating array of duplicates, finding newest asof, , removing others db table value. however, feel linq has ability able that, possibly comparator. have basic knowledge on it.
any ideas?
unless really need modify original datatable
, create projection give records in each dt
group has "newest" asof
value:
var rows = dt.asenumerable() .orderby(row => row.field<sqldatetime>("asof")) .groupby(row => row.field<sqldatetime>("dt")) .select(rg => rg.first()) .tolist();
then either clear original data table , replace these rows or create new datatable
based on these rows , swap original.
another option filter data before it's loaded data table, since didn't specify how happens can't offer guidance there.
Comments
Post a Comment