动态CRM:包含<>不适用于CRM

时间:2015-05-05 12:45:56

标签: linq dynamics-crm

我的代码如下:

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();

但是在上面我使用的查询包含它给出计数0.即使我在列表中有共同的记录

1 个答案:

答案 0 :(得分:1)

Contains Linq扩展方法不知道如何比较复杂对象。

将f.OpportunityId与Guids列表而不是匿名类型列表进行比较将起作用:

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

在自定义比较器类中实现IEqualityComparer以比较复杂对象:https://stackoverflow.com/a/6694563/1817350

请记住,调用.ToList()会将所有记录记录到内存中,并会导致大量记录出现性能问题。