0

I have a list (List A) of custom Dynamics CRM objects and then I want to create another list (List B) of the same custom object type which will NOT include any custom object which already exists in List A. The unique instance identifier is a parameter called "emailaddress"

I have tried making the lists and then doing a "remove" (or exclude) on List B - this works but seems to be slow and I would rather try to keep any duplicate out them out at initial list (B) creation-time - i.e. as part of the query used to create List B. Does anyone know if this is possible? (C#)

(Assuming List A has already been instantiated) The psuedo/code would be like

...
from b in context.CreateQuery("contact")
where (string) b["emailaddress"] !=null 
and b["emailaddress"] NOT IN ListA
select new object.....toList()

Thanks

  • If I'm understanding correctly you're after a left outer join.. see here https://stackoverflow.com/questions/3404975/left-outer-join-in-linq – Sean T Dec 06 '17 at 15:07

1 Answers1

0

I think a left outer join will do the trick...

var q =
(from b in context.CreateQuery("contact")
join c in ListA on c.emailaddress equals b.emailaddress into bc
from c in bc.DefaultIfEmpty()
select new { whatever your object properties are}).ToList();
Sean T
  • 2,414
  • 2
  • 17
  • 23
  • Thanks, Sean, but I am not offered the emailaddress attribute ("on c.emailaddress") - or any attribute of c - when I try this? – user2996958 Dec 06 '17 at 15:34
  • If I change the code slightly so no compile errors - to var q = (from b in context.CreateQuery("contact") join c in ListtA on (string)b["emailaddress"] equals c.email into bc from c in bc.DefaultIfEmpty() select new ContactData {.... .I get the error "Invalid 'join' condition. An entity member is invoking an invalid property or method. – user2996958 Dec 06 '17 at 15:46
  • can you post the properties of the contact object and ListA object in your answer and I'll amend – Sean T Dec 06 '17 at 16:07
  • Thanks Sean. ListA already exists - it is a list of custom "ContactData" objects and the unique identifier attribute for this object is called 'email'. When creating ListB I am creating a new list of ContactData objects from CRM (context.CreateQuery("contact")) whose source email attribute is called "emailaddress1", so it will be "Add to ListB IF ListA.email does not contain the incoming/to be evaluated ["emailaddress1"] value. I hope this makes sense - thanks for your help – user2996958 Dec 07 '17 at 09:48