比如如下DataTable:
DW DADUI PERSON
1 2 john
1 3 jack
1 3 tom
1 2 jordan
2 3 dennis
要求实现如下处理:
如果DW与DADUI均相同,则仅保留一列。上表处理后如下:
DW DADUI PERSON
1 2 john
1 3 jack
2 3 dennis
请问上述如何实现?
------解决方案--------------------------------------------------------
- C# code
void Main(){ DataTable dt = new DataTable(); dt.Columns.Add("DW",typeof(int)); dt.Columns.Add("DADUI",typeof(int)); dt.Columns.Add("PERSON"); dt.Rows.Add(1,2,"john"); dt.Rows.Add(1,3,"jack"); dt.Rows.Add(1,3,"tom"); dt.Rows.Add(1,2,"jordan"); dt.Rows.Add(2,3,"dennis"); var result=from d in dt.AsEnumerable() group d by new {DW=d.Field<int>("DW"),DADUI=d.Field<int>("DADUI")} into g select new { DW=g.Key.DW, DADUI=g.Key.DADUI, PERSON=g.FirstOrDefault().Field<string>("PERSON") }; DataTable dtNew=dt.Clone(); result.ToList().ForEach(d=>dtNew.Rows.Add(d.DW,d.DADUI,d.PERSON)); }