Если вы хотите выбрать все элементы, которые имеют пустые поля NPI
, вместе с уникальными полями NPI
из остальных, вы можете использовать предложение GroupBy
вместе с некоторыми проверками на пустые значения NPI
:
var people = new List<Person>
{
new Person ("dupe", "dupe npi"),
new Person ("dupe", "another dupe npi"),
new Person ("", "empty npi"),
new Person ("", "another empty npi"),
new Person ("unique", "unique npi")
};
var uniqueAndBlank = people
.Where(p => string.IsNullOrEmpty(p.getNPI())) // Get all the empty NPIs
.Concat(people // Concat with other items
.Where(p => !string.IsNullOrEmpty(p.getNPI())) // Whose NPIs are set
.GroupBy(p => p.getNPI()) // Grouped by their NPI fields
.Select(g => g.First())) // Grab the first item
.ToList();
// Result:
// {{ "", "empty npi" },
// { "", "another empty npi" },
// { "dupe", "dupe npi" },
// { "unique", "unique npi" }}