I have this LINQ query (sorry it;s a long one) that query sets from CRM 2011. Right now it pulls down data, but it doesn’t include the ones with NULLs I want it to pull down the ones with and without NULLs. Anyway to do this with this query. I’m kinda stuck. Thanks!
Update: I’ve updated the code to include the two left joins to include NULLs. But now I am getting this error:
“The ‘GroupJoin’ operation must be followed by a ‘SelectMany’ operation where the collection selector is invoking the ‘DefaultIfEmpty’ method.”
Any ideas on how to fix the error?
var linqQuery = (from r in gServiceContext.CreateQuery("opportunity")
join c in gServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"] into opp
join n in gServiceContext.CreateQuery("annotation") on r["opportunityid"] equals ((EntityReference)n["objectid"]).Id into notes
from o in opp.DefaultIfEmpty()
from nt in notes.DefaultIfEmpty()
where ((EntityReference)r["new_channelpartner"]).Id.Equals(lProfileProperty.PropertyValue) && ((OptionSetValue)r["new_leadstatus"]).Equals("100000002")
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_sourcepick") ? string.Empty : r.FormattedValues["new_sourcepick"],
CreatedOn = !r.Contains("createdon") ? string.Empty : ((DateTime)r["createdon"]).ToShortDateString(),
CreatedOnSort = !r.Contains("createdon") ? string.Empty : ((DateTime)r["createdon"]).Ticks.ToString(),
State = !o.Contains("address1_stateorprovince") ? string.Empty : ((String)o["address1_stateorprovince"]),
Zip = !o.Contains("address1_postalcode") ? string.Empty : ((String)o["address1_postalcode"]),
Eval = !r.Contains("new_distributorevaluation") || ((OptionSetValue)r["new_distributorevaluation"]).Value.ToString() == "100000000" ? "NA" : r.FormattedValues["new_distributorevaluation"].Substring(0, 2),
EvalVal = !r.Contains("new_distributorevaluation") ? "100000000" : ((OptionSetValue)r["new_distributorevaluation"]).Value.ToString(),
DistributorName = !r.Contains("new_channelpartner") ? string.Empty : ((EntityReference)r["new_channelpartner"]).Name,
ContactStreetAddress = !o.Contains("address1_line1") ? string.Empty : o["address1_line1"],
ContactStreetAddress2 = !o.Contains("address1_line2") ? string.Empty : o["address1_line2"],
ContactCity = !o.Contains("address1_city") ? string.Empty : o["address1_city"],
ContactState = !o.Contains("address1_stateorprovince") ? string.Empty : o["address1_stateorprovince"],
ContactZip = !o.Contains("address1_postalcode") ? string.Empty : o["address1_postalcode"],
ContactCountry = !o.Contains("address1_country") ? string.Empty : o["address1_country"],
ContactPhone = !o.Contains("telephone1") ? string.Empty : o["telephone1"],
ContactMobilePhone = !o.Contains("mobilephone") ? string.Empty : o["mobilephone"],
ContactEmail = !o.Contains("emailaddress1") ? string.Empty : o["emailaddress1"],
Notes = !r.Contains("new_distributornotes") ? string.Empty : r["new_distributornotes"],
EstimatedCloseDate = !r.Contains("estimatedclosedate") ? string.Empty : r["estimatedclosedate"],
MaturityValue = !r.Contains("estimatedvalue") ? string.Empty : ((Money)r["estimatedvalue"]).Value.ToString(),
DistributorStatus = !r.Contains("new_distributorstatuspicklist") ? "Unopened" : r.FormattedValues["new_distributorstatuspicklist"],
ColderNotes = !nt.Contains("notetext") ? string.Empty : nt["notetext"],
ColderNotesCreatedOn = !nt.Contains("createdon") ? string.Empty : ((DateTime)nt["createdon"]).ToShortDateString(),
ColderNotesCreatedBy = !nt.Contains("createdby") ? string.Empty : ((EntityReference)nt["createdby"]).Name,
});
You’ll need to change both of your joins to LEFT Joins. You can follow the example here
There are other syntaxes, but this should work for you.
Edit: Here is the syntax I use for this (sterilized so I hope I didn’t break anything important). It’s a bit different but I find it easier to work with. Hopefully this will help: