0

My code is as below:

var conntionRecord1Id = (from connectionBase in orgServiceContext.CreateQuery("connection")
where connectionBase["record1roleid"] == null
select new { OpportunityId = connectionBase["record1id"] }).Distinct().ToList();

var query =
    from opportunity in orgServiceContext.CreateQuery("opportunity")
    orderby opportunity["createdon"] ascending
    select new
    {
        Topic = opportunity.Attributes.Contains("name") == true ? opportunity["name"] : null,
        OpportunityId = opportunity.Attributes.Contains("opportunityid") == true ? opportunity["opportunityid"] : null,
        PostalCode = opportunity.Attributes.Contains("new_address_postalcode") == true ? opportunity["new_address_postalcode"] : null,
    };

var result = (from f in query.ToList() where conntionRecord1Id.Contains(f.OpportunityId) select f).ToList();

But in above query where i am using Contains it giving count 0. even though I have common records in the list

Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42
yogs
  • 23
  • 1
  • 9

1 Answers1

1

The Contains Linq extension method does not know how to compare complex objects.

Comparing f.OpportunityId to a list of Guids instead of a list of an anonymous type will work:

var conntionRecord1Id = (from connectionBase in orgServiceContext
.CreateQuery("connection") 
where connectionBase["record1roleid"] == null 
select connectionBase.GetAttributeValue<Guid?>("record1id"))
.Distinct()
.ToList();   

Implement IEqualityComparer in a custom comparer class to compare complex objects: https://stackoverflow.com/a/6694563/1817350

Keep in mind that calling .ToList() brings down all the records into memory and will cause performance problems with large amounts of records.

Community
  • 1
  • 1
Bvrce
  • 2,170
  • 2
  • 27
  • 45