使用LinQ我想查询列表并查找重复的人(副本被定义为具有相同的姓氏和姓氏和出生日期),并使用字符串"duplicate"标记每个重复人员的StateOfData属性,并且每个都是唯一的person的StateOfData属性,字符串"unique".
public Class Person
{
public string PersonFirstName { get; set; }
public string PersonLastName { get; set; }
public datetime PersonDateOfBirth { get; set; }
public string StateOfData{ get; set }
public Person (string firstName, string lastName, dateTime dateOfBirth,string state)
{
this.PersonFirstName = firstName;
this.PersonLastName = lastName;
this.PersonDateOfBirth = dateOfBirth;
this.StateOfData = state;
}
}
IList<Person> personsList = new List<Person>();
Person pers = new Person("Diane","Jones","1967-01-01","");
personsList.add(pers);
Person pers = new Person("Diane","Jones","1967-01-01","");
personsList.add(pers);
Person pers = new Person("John","Jones","1967-01-01","");
personsList.add(pers);
Run Code Online (Sandbox Code Playgroud)
人员名单的结果应为:
"黛安","琼斯","1967-01-01","复制",
"黛安","琼斯","1967-01-01","复制",
"约翰","琼斯","1967-01 -01" , "独一无二"
任何有关这方面的帮助将不胜感激
var theLookup = personList
.GroupBy(p => new {p.PersonFirstName, p.PersonLastName, p.PersonDateOfBirth})
.ToLookup(g => g.Skip(1).Any() ? "duplicate" : "unique");
foreach(var lookupEntry in theLookup)
{
string stateOfData = lookupEntry.Key;
foreach(Person p in lookupEntry.SelectMany(g => g))
{
p.StateOfData = stateOfData;
}
}
Run Code Online (Sandbox Code Playgroud)