I have a list 'nList' returned from a LINQ query. The data is correct but I have to check if the data in the 2nd field ('nType') occurs more than once, and if it does, remove it from the list if the 3rd field ('nAmount') is null. To illustrate, in the example below, I would want to remove the record with ID 2. Can this be done using LINQ?
EDIT: There can only be 2 scenarios:
- one occurrence of nType, with nAmount populated
- two occurences of nType, with one nAmount populated, the other nAmount null
ID nType nAmount
1 A 12.00
2 A
3 B 13.00
4 C 14.00
This can be done with a
GroupBy
overload which lets you define the grouped objectLive example: http://rextester.com/PFX41986
This has a few caveats
nType
which has a nullnAmount
it will not appear in the resultsnType
with a non-nullnAmount
this will take the first one.You would need to install MoreLinq from Nuget to get the
DistinctBy()
.