1

I have a class:

public class CustomerEmailAlert
{
  public string EmailId { get; set; }
  public string Interest1 {get;set;}
  public string Interest2 {get;set;}     
}

The result from sql is something like this: +---------------+-----------+-----------+ | Email | Interest1 | Interest2 | +---------------+-----------+-----------+ | abc@gmail.com | Burrito | Apple | | abc@gmail.com | Pizza | Milk | | abc@gmail.com | Apple | Burrito | | def@gmail.com | Milk | Banana | +---------------+-----------+-----------+

I have mapped the result using Dapper to List<CustomerEmailAlert>

List<CustomerEmailAlert>= con.Query<CustomerEmailAlert>("getalerts", commandType: CommandType.StoredProcedure).ToList();

My question is: How do I group Customers by Email so that the email they receive contains their interests (they should receive only 1 copy of email)

I have tried this: Group by in LINQ

Community
  • 1
  • 1
afrose
  • 169
  • 1
  • 9

1 Answers1

1

You could group this way:

var result= from e in list
            group new {e.Interest1,e.Interest2} by e.Email into g
            select new {Email=g.Key, Interests=g.ToList()};
ocuenca
  • 38,548
  • 11
  • 89
  • 102