我一直在使用这个:
return myListOfContacts.DistinctBy(e => e.Id).Where(e => e.CompanyId == companyId).ToList();
这会返回一个请求的去重列表。问题是,当记录是重复时,返回的记录是列表中的第一个。
例如,如果我的联系人中有以下联系人:
[Id - ContactId - Name - FlagIWantThisOne]
1 - 99 - John - true
2 - 56 - Mike - false
2 - 56 - Mike - true
3 - 13 - Dave - false
它返回3条记录:
John、Mike和Dave。
但我想要的是标记为true的那个“Mike”记录。
总之,如果记录重复,则列表应返回设置为true的记录并忽略其他记录。
我使用了 distinctBy
,但它只返回列表中找到的第一个记录。