//.net,c#,List,Lambda 排除重复直接看代码: //场景1,有以下的数据,现要按name列排除重复,使name列唯一 List<Model> li1 = new List<Model>() { new Model { id = "1111", name = "name1", gender=20 }, new Model { id = "2222", name = "name2", gender = 21 }, new Model { id = "3333", name = "name1", gender = 22 }, new Model { id = "4444", name = "name2", gender = 23 }, new Model { id = "5555", name = "name5", gender = 25 }, }; //场景2,有以下的数据,现要把每行完全一样的去掉,使数据唯一 List<Model> li2 = new List<Model>() { new Model { id = "1111", name = "name1", gender = 20 }, new Model { id = "1111", name = "name1", gender = 20 }, new Model { id = "2222", name = "name1", gender = 20 }, new Model { id = "1111", name = "nnnn1", gender = 20 }, new Model { id = "1111", name = "name1", gender = 22 }, }; /* 1. * 排除重复后只输出单列,结果: * name = "name1";name = "name2";name = "name5" */ var ol = li1.GroupBy(p => p.name).Distinct();// /*2. * name列不重复 * 排除重复后输出多列.结果: * id = "1111", name = "name1",gender=20 * id = "2222", name = "name2",gender=21 * id = "5555", name = "name5",gender=25 */ var ol2 = li1.GroupBy(p => p.name).Select(p => p.First()); /*3. * name列不重复 * 排除重复后.选择输出id,gender两列.结果: * id = "1111",gender=20 * id = "2222",gender=21 * id = "5555",gender=25 */ var ol3 = li1.GroupBy(p => p.name).Select(p => p.First()).Select(p => new { p.id,p.gender });// foreach (var item in ol) { Response.Write(item.Key + "|");//1111,name1,20|2222,name2,21|5555,name5,25| } Response.Write("<hr/>"); foreach (var item in ol2) { Response.Write(item.id + "," + item.name + "," + item.gender + "|");//1111 name1|2222 name2|5555 name5| } //场景2的排除法 var ol4 = li2.GroupBy(p => new { p.name,p.id,p.gender }).Select(p => p.First()); //model public class Model { public string id { get; set; } public string name { get; set; } public int gender { get; set; } }