简体   繁体   English

LINQ错误使用DefaultIfEmpty时

[英]LINQ Error When using DefaultIfEmpty

I am trying to pull down the NULLS of the contacts and I am using DefaultIfEmpty to do it. 我试图拉下联系人的NULLS,我正在使用DefaultIfEmpty来做。 But I am getting this error. 但是我收到了这个错误。

"The method 'GroupJoin' cannot follow the method 'Join' or is not supported. Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' method before calling unsupported methods." “方法'GroupJoin'不能遵循方法'Join'或不支持。尝试根据支持的方法编写查询,或者在调用不支持的方法之前调用'AsEnumerable'或'ToList'方法。”

I am using the LINQ-to-CRM provider and querying from the CRM 2011 Web Service. 我正在使用LINQ-to-CRM提供程序并从CRM 2011 Web Service查询。

This is the code I am using: 这是我正在使用的代码:

var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")
         join a in orgServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals a["accountid"]
         join c in orgServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"]
         where ((EntityReference)r["new_channelpartner"]).Id.Equals(new Guid("c55c2e09-a3be-e011-8b2e-00505691002b"))
         select new
         {
           OpportunityId = !r.Contains("opportunityid") ? string.Empty : r["opportunityid"],
           CustomerId = !r.Contains("customerid") ? string.Empty : ((EntityReference)r["customerid"]).Name,
           Priority = !r.Contains("opportunityratingcode") ? string.Empty : r.FormattedValues["opportunityratingcode"],
           ContactName = !r.Contains("new_contact") ? string.Empty : ((EntityReference)r["new_contact"]).Name,
           Source = !r.Contains("new_source") ? string.Empty : r["new_source"],
           CreatedOn = !r.Contains("createdon") ? string.Empty : r["createdon"],
           State = !a.Contains("address1_stateorprovince") ? string.Empty : a["address1_stateorprovince"],
           Zip = !a.Contains("address1_postalcode") ? string.Empty : a["address1_postalcode"],
           Eval = !r.Contains("new_colderevaluation") ? string.Empty : r.FormattedValues["new_colderevaluation"],
           DistributorName = !r.Contains("new_channelpartner") ? string.Empty : ((EntityReference)r["new_channelpartner"]).Name,
           ContactStreetAddress = !c.Contains("address1_line1") ? string.Empty : c["address1_line1"]
         });

How would I go about getting rid of this error? 我该怎么做才能摆脱这个错误? Any help would be awesome. 任何帮助都是极好的。

Thanks! 谢谢!

Since your LINQ query must ultimately translate to a valid QueryExpression, you can't do some of the fancier projection stuff because the OrganizationDataContext isn't smart enough to run the simple QueryExpression and then apply your fun " fieldname = !c.Contains("fieldname") ? string.Empty : c["fieldname"] " in memory. 由于您的LINQ查询必须最终转换为有效的QueryExpression,因此您无法执行一些更高级的投影内容,因为OrganizationDataContext不够智能,无法运行简单的QueryExpression,然后应用您的乐趣“ fieldname = !c.Contains("fieldname") ? string.Empty : c["fieldname"] ”在内存中。 You have to do that yourself. 你必须自己做。

So, in your first query, just do: 所以,在你的第一个查询中,只需:

var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")     
    // snip
    select new
    {
      fieldname = c["fieldname"],
      //etc...
    });

Then do something like: 然后做一些事情:

var linqQuery2 = from r in linqQuery.ToList()
                 select new
                 {
                   fieldname = r["fieldname"] == null ? string.Empty : r["fieldname"],
                   //(etc)...
                 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM