I have a list of orders and need to create groups of similar orders in an efficient manner (preferably without pulling back all orders and comparing manually).
Each order has a UserId and Email Address with additional fields of Postcode and Country.
So I need to create groups on the following rules:
If an order has the same (UserId or Email Address) and (postcode and country) place in a group
Given the following entity and data
public class Order
{
public int UserId { get; set; }
public int OrderId { get; set; }
public string Email { get; set; }
public int PostCode { get; set; }
public string Country { get; set; }
}
Example data
OrderId UserId Email PostCode Country
1 1 blah1 111 au
2 1 blah2 111 au
3 2 blah1 111 au
4 2 blah2 111 au
5 3 blah3 111 nz
6 3 blah3 111 nz
Example Results
Group 1
1 1 blah1 111 au
2 1 blah2 111 au
3 2 blah1 111 au
4 2 blah2 111 au
Group 2
5 3 blah3 111 nz
6 3 blah3 111 nz
The only way I can seem to think of doing this through manual iteration in memory
Is this possible to do with Linq to entities cleanly?
Update
After researching this for a while, I think I've come to the conclusion the only way to achieve what I want is to do two groupbys and manually combine them in memory.
Update 2
Thinking about this logically there is seemingly no elegant solution for this problem in linq and may need to be done with SQL and CTEs or some other recursive solution.