简体   繁体   English

LINQ:具有多个条件的左外连接

[英]LINQ: Left Outer Join with multiple conditions

I have two IEnumerables called BaseReportDefinitions and InputReportDefinitions. 我有两个IEnumerables,分别称为BaseReportDefinitions和InputReportDefinitions。 I need to do a left outer join where i want all the InputReportDefinitions and whichever BaseReportDefinitions that match. 我需要做一个左外部连接,在这里我想要所有的InputReportDefinitions和匹配的BaseReportDefinitions。 Both IEnumberables contain ReportDefinition objects that contain ParentName and ReportName properties that need to be used as the join key. 这两个IEnumberables都包含ReportDefinition对象,这些对象包含需要用作联接键的ParentName和ReportName属性。 I want to return the ReportDefinition object for each (in the case of BaseReportDefinition entry it may be null) in an anonymous object. 我想为匿名对象中的每个对象返回ReportDefinition对象(在BaseReportDefinition条目的情况下,它可能为null)。

I have seen many examples of linq outer joins and outer joins with a static second condition that often gets put into a where condition but nothing that really uses two conditions fully for the join. 我已经看到了很多linq外部联接和带有静态第二个条件的外部联接的示例,这些条件通常被置于where条件中,但是没有一个真正为联接完全使用两个条件的情况。

var items = inputReportDefinitions.GroupJoin(
              baseReportDefinitions,
              firstSelector => new {
                         firstSelector.ParentName, firstSelector.ReportName
                                   },
              secondSelector => new {
                         secondSelector.ParentName, secondSelector.ReportName
                                   },
              (inputReport, baseCollection) => new {inputReport, baseCollection})
              .SelectMany(grp => grp.baseCollection.DefaultIfEmpty(),
                         (col, baseReport) => new
                                                 {
                                                    Base = baseReport,
                                                    Input = col.inputReport
                                                 });

I believe this ends up being a left outer join. 我相信这最终将成为左外部联接。 I don't know how to convert this monstrosity to a query statement. 我不知道如何将这种怪异转换为查询语句。 I think if you add AsQueryable() to the end it could be used in Linq-to-SQL, but honestly, I have little experience with that. 我认为,如果将AsQueryable()添加到最后,则可以在Linq-to-SQL中使用它,但是老实说,我对此几乎没有经验。

EDIT: I figured it out. 编辑:我想通了。 Much easier to read: 更容易阅读:

var otherItems = from i in inputReportDefinitions
                         join b in baseReportDefinitions
                         on new {i.ParentName, i.ReportName} 
                         equals new {b.ParentName, b.ReportName} into other
                         from baseReport in other.DefaultIfEmpty()
                         select new
                                    {
                                        Input = i,
                                        Base = baseReport
                                    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM