带有 NULL 的 LINQ 查询

发布于 2024-12-23 15:31:58 字数 4113 浏览 1 评论 0原文

我有一个从 CRM 2011 查询集的 LINQ 查询(抱歉;很长)。现在它会拉取数据,但它不包括带有 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,
});

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光匆匆的小流年 2024-12-30 15:31:58

您需要将两个连接更改为 LEFT 连接。您可以按照此处的示例进行操作

还有其他语法,但这应该可以工作为你。

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

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

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:

        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] });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文