LINQ查询与NULL

时间:2011-12-30 16:57:28

标签: c# linq dynamics-crm dynamics-crm-2011

我有这个LINQ查询(对不起,很长一点)查询从CRM 2011设置。现在它下拉数据,但它不包括那些带有NULL的我希望它用下拉的那些没有NULL。无论如何使用此查询执行此操作。我有点卡住了。谢谢!

更新:我已更新代码以包含两个左连接以包含NULL。但现在我收到了这个错误:

“'GroupJoin'操作必须后跟'SelectMany'操作,其中集合选择器正在调用'DefaultIfEmpty'方法。”

有关如何修复错误的任何想法?

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,
});

1 个答案:

答案 0 :(得分:1)

您需要将两个联接更改为LEFT Joins。您可以按照示例here

进行操作

还有其他语法,但这应该对你有用。

编辑:这是我用于此的语法(消毒,所以我希望我没有打破任何重要的事情)。它有点不同但我发现它更容易使用。希望这会有所帮助:

        var x =
            (from A in db.Table1
             from B in db.Table2
            .Where(p => p.Table1_ID == A.ID)
            .DefaultIfEmpty()
            from C in db.Table3
            .Where(c => c.Table2_ID == B.ID)
            .DefaultIfEmpty()
            where A.field1 == value1
            select new { [fieldlist] });