I have a class with multiple properties, of which I'm interested in two. Say, PropA
and PropB
in the following example.
public class GroupByOR
{
public string PropA { get; set; }
public string PropB { get; set; }
public int SomeNumber { get; set; }
public GroupByOR(string a, string b, int num) { PropA = a; PropB = b; SomeNumber = num; }
}
And I would like to group a list of this class' objects, criteria being an item should fall into a group if either PropA
or PropB
matches.
For example, let's say my list looks like this:
List<GroupByOR> list = new List<GroupByOR>
{
new GroupByOR("CA", "NY", 1), // Item 1
new GroupByOR("CA", "OR", 2), // Item 2
new GroupByOR("NY", "OR", 5) // Item 3
};
Then my desired outcome is this:
Group 1: Items 1 and 2 (based on CA)
Group 2: Items 1 and 3 (based on NY)
Group 3: Items 2 and 3 (based on OR)
Looking around, I found this and many other examples, but they all seem to focus on grouping by multiple properties, but with an AND
operation.
Is what I'm trying to achieve even possible?
Or else is join
the way to go here? If so can you please direct me in the right direction?