使用LINQ with CRM选择Distinct

时间:2012-06-06 15:14:35

标签: c# linq distinct

我正在尝试使用LINQ to CRM查询选择Distinct set,并继续返回dupilicate记录。我正在使用的查询是:

  var linqQuery = (from r in gServiceContext.CreateQuery("opportunity")
                   join c in gServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals c["accountid"]
                   join u in gServiceContext.CreateQuery("systemuser") on ((EntityReference)r["ownerid"]).Id equals u["systemuserid"]
                   where (r["statuscode"].Equals("100000004") || r["statuscode"].Equals("100000003")) && r["statecode"].Equals("Open")
                   where u["internalemailaddress"].Equals(_currentUser.Email)

                       select new
                           {
                                 AccountId = !c.Contains("accountid") ? string.Empty : c["accountid"],
                                 Account = !c.Contains("name") ? string.Empty : c["name"]
                             }).Distinct();

我错过了让.Distinct()工作的东西吗?或者有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

使用指定的IEqualityComparer<T>http://msdn.microsoft.com/en-us/library/bb356803

另一件事,我不确定匿名类是否支持IEqualityComparer<T>实现。

答案 1 :(得分:1)

没有显式比较器的Distinct调用只使用默认的相等比较器,在匿名类型的情况下归结为Object.Equals。如果类型的所有属性相等,则在匿名类型中重写此值将相等。在这种情况下,它会检查AccountIdAccount属性。

我怀疑这是有效的,并且值在案例方面是不同的(因为字符串在默认情况下比较区分大小写)或者它们实际上是不同的,你只是看不到它。

幸运的是,它与特定于CRM的Linq提供商无关。

相关问题