我的代码如下:
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.即使我在列表中有共同的记录
答案 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()会将所有记录记录到内存中,并会导致大量记录出现性能问题。