afrose
afrose

Reputation: 169

Group by in linq for email alert

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 | +---------------+-----------+-----------+ | [email protected] | Burrito | Apple | | [email protected] | Pizza | Milk | | [email protected] | Apple | Burrito | | [email protected] | 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

Upvotes: 1

Views: 66

Answers (1)

ocuenca
ocuenca

Reputation: 39346

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()};

Upvotes: 1

Related Questions