LINQ查询独占返回重复项

时间:2015-07-21 20:33:49

标签: linq crm microsoft-dynamics

我正在研究这个LINQ查询。我希望结果列表根据EMailAddress1字段返回一个包含重复项的记录列表,并按EMailAddress1字段分组。

例如:

  1. emailaddress1@gmail.com
  2. emailaddress1@gmail.com
  3. emailaddress2@gmail.com
  4. emailaddress2@gmail.com
  5. emailaddress2@gmail.com
  6. emailaddress3@gmail.com
  7. emailaddress3@gmail.com
  8. 对此有何建议?谢谢。

     var contacts = (from c in xrm.ContactSet 
                            where c.StateCode != 1 
                            orderby c.EMailAddress1, c.CreatedOn 
                            descending select new { 
                                c.FirstName, 
                                c.LastName, 
                                c.EMailAddress1, 
                                c.ContactId, 
                                c.CreatedOn }).ToList();
    

1 个答案:

答案 0 :(得分:0)

根据您之前的查询:

var duplicatedEmails = (from c in contacts
                        group c by c.EMailAddress1 into g
                        where g.Count() > 1
                        select g.Key).ToList();
var duplicatedContacts = contacts.Where(c => duplicatedEmails.Contains(c.EMailAddress1));