c# - Use LINQ to find duplicated rows (with list of specified columns) -
i use code below duplicated rows 3 columns: string, date, money. wonder if there general method can input dynamic list of column name in linq find duplicated rows?
datatable allduplicates = dt.asenumerable() .groupby(dr => new { field1 = dr.field<object>("string"), field2 = dr.field<object>("date"), field3 = dr.field<object>("money"), }) .where(g => g.count() > 1) .selectmany(g => g) .tolist().copytodatatable(); }
how custom arrayequalitycomparer<t> type (such 1 listed here):
string[] colstoconsider = ... var allduplicates = dt.asenumerable() .groupby(dr => colstoconsider.select(dr.field<object>) .toarray(), new arrayequalitycomparer<object>()) .where(g => g.count() > 1) .selectmany(g => g) .copytodatatable(); you can consider using dictionary<tkey, tvalue> (and associated dictionary-comparer) if find implicit use of array indices here hackish.
Comments
Post a Comment