0

I'm trying to group my data by using multiple columns in that way :

var groupedCustomers = listToProcess.GroupBy(cn => cn.CUST_NAME).Select(g => g.ToList()).ToList();

Grouping like that, I'm satisfied of what I'm getting as I can loop through all my objects and getting all their properties. However, as I'm supposed to group my table data using two fields (cust_name and cust_address), here's what I'm trying to do :

var groupedCustomers = listToProcess.GroupBy(cn => cn.CUST_NAME, ca => ca.CUST_ADDRESS).Select(g => g.ToList()).ToList();

Processing like that is also working but for each object, I'm only getting the address property.

Is there any other method to do that?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Traffy
  • 2,803
  • 15
  • 52
  • 78

2 Answers2

1

You can use an anonymous type as composite key:

var groupedCustomers = listToProcess
       .GroupBy(cn => new { cn.CUST_NAME, cn.CUST_ADDRESS })
       .Select(g => g.ToList()).ToList();
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

try

   var groupedCustomers = listToProcess
           .GroupBy(cn => new {CUST_NAME = cn.CUST_NAME, CUST_ADDRESS  = cn.CUST_ADDRESS })
           .Select(g => g.ToList()).ToList();
Damith
  • 62,401
  • 13
  • 102
  • 153