I have a list of items that need to be grouped on 3 fields. However, I need to include items in a group if some of these fields are null, but the rest match. It's kind of hard to explain, but here is a scenario.
So for example, we have a list of the following:
Items:
Amount Type Code
50 Test AA
50 Test2 BB
null Test AA
45 Test3 CC
50 Test null
50 Test AB
So we would get 4 groups
Group 1: 50/null + Test + AA/null
Group 2: 50 + Test2 + BB
Group 3: 45 + Test3 + CC
Group 4: 50 + Test + AB
We include the null rows in Group 1 because though we had null values, the rest was a match. I'm not sure how I can accomplish this via linq or C# and could use some help!
It took me a few re-reads to see what you meant. But, I think you mean to group by each of the list object's properties, while ignoring null values in the property compare.
So here's a solution by using a custom equality comparer (called
ItemComparerin the code sample below) that meets your special requirement. You can then plug this comparer into the linq GroupBy function and voila:This returns the following: