RE: query entity records which are not in other collection ?
Hey there,
You're right in that the CRM context does not support In / Not In queries. This is however a pure CRM context limitation, Linq itself can handle that perfectly well.
If you have some other filter criteria, you could just apply those filters and call ToList afterwards. After that you can apply your contains / contains not filter, as you have already retrieved the records with ToList, so that all following filters will be applied locally.
So this would execute without errors:
var result = context.CreateQuery("contract")
.ToList()
.Where(a => !results2.Entities.Contains(a.Attributes["contractid"]) )
.ToList();
However if you don't have prefilters, like above, this is not a good idea performance wise, as it will retrieve all contacts from CRM, which can be quite a lot. In this case, either use a QueryExpression in combination with RetrieveMultiple, or FetchXml.
Kind Regards,
Florian