在linq中分组以获取电子邮件警报

时间:2017-03-02 18:55:47

标签: c# linq

我有一个班级:

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

sql的结果是这样的: +---------------+-----------+-----------+ | Email | Interest1 | Interest2 | +---------------+-----------+-----------+ | abc@gmail.com | Burrito | Apple | | abc@gmail.com | Pizza | Milk | | abc@gmail.com | Apple | Burrito | | def@gmail.com | Milk | Banana | +---------------+-----------+-----------+

我已使用Dapper将结果映射到List<CustomerEmailAlert>

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

我的问题是:如何通过电子邮件对客户进行分组,以便他们收到的电子邮件包含他们的兴趣(他们应该只收到 1份电子邮件

我试过这个:Group by in LINQ

1 个答案:

答案 0 :(得分:1)

你可以这样分组:

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