簡體   English   中英

LINQ錯誤使用DefaultIfEmpty時

[英]LINQ Error When using DefaultIfEmpty

我試圖拉下聯系人的NULLS,我正在使用DefaultIfEmpty來做。 但是我收到了這個錯誤。

“方法'GroupJoin'不能遵循方法'Join'或不支持。嘗試根據支持的方法編寫查詢,或者在調用不支持的方法之前調用'AsEnumerable'或'ToList'方法。”

我正在使用LINQ-to-CRM提供程序並從CRM 2011 Web Service查詢。

這是我正在使用的代碼:

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"]
         });

我該怎么做才能擺脫這個錯誤? 任何幫助都是極好的。

謝謝!

由於您的LINQ查詢必須最終轉換為有效的QueryExpression,因此您無法執行一些更高級的投影內容,因為OrganizationDataContext不夠智能,無法運行簡單的QueryExpression,然后應用您的樂趣“ fieldname = !c.Contains("fieldname") ? string.Empty : c["fieldname"] ”在內存中。 你必須自己做。

所以,在你的第一個查詢中,只需:

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

然后做一些事情:

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