C# 判断list类型中某字段是否存在相同值
新建model
public class Student { public int ID { get; set; } public string Name { get; set; } public string Class {get; set; } }
新建list
List<Student> stu = new List<Student>();
判断name是否重复 /////// 判断name与class是否都重复
bool isRepeat = stu.GroupBy(i => i.Name).Any(g => g.Count() > 1);
bool isRepeat = stu.GroupBy(i => new { m.Name, m.Class}).Any(g => g.Count() > 1);
isRepeat 为true 存在重复,false 不存在