Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Error When using DefaultIfEmpty

I am trying to pull down the NULLS of the contacts and I am using DefaultIfEmpty to do it. 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."

I am using the LINQ-to-CRM provider and querying from the 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!


1 Answers

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. 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)...
                 };
like image 57
Josh Painter Avatar answered Jul 30 '26 14:07

Josh Painter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!