0

I'm working on this LINQ query. I'd like the resulting list return a list of records that contain duplicates exclusively, based on the EMailAddress1 field and grouped by the EMailAddress1 field.

For instance:

  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

etc.

Any advice on this? Thanks.

 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();
  • It is neccesary to be a Query? Why not use the native one "find duplicates" – Sxntk Jul 21 '15 at 21:00
  • @Sxntk Well I'm doing other things with this list....I suppose I could iterate over each element in the list and compare each other, like this:foreach (var c in contacts) { foreach (var b in contacts) if (b == c) { //Do something } } But what about the grouping? Is there way I can group each "EMailAddress1" field in the list? – user4946742 Jul 21 '15 at 21:33
  • You can't group with LinQ CRM, you can query all the records on account and with that list use the normal LinQ – Sxntk Jul 21 '15 at 21:36

1 Answers1

0

Based on your previous query:

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));
Ken Hung
  • 190
  • 6