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

发布于 2024-12-18 15:28:53 字数 374 浏览 3 评论 0原文

我有两个 IEnumerables,称为 BaseReportDefinitions 和 InputReportDefinitions。我需要做一个左外连接,我想要所有的 InputReportDefinitions 和匹配的 BaseReportDefinitions。两个 IEnumberable 都包含 ReportDefinition 对象,该对象包含需要用作连接键的 ParentName 和 ReportName 属性。我想为匿名对象中的每个(在 BaseReportDefinition 条目的情况下它可能为空)返回 ReportDefinition 对象。

我见过许多 linq 外连接和具有静态第二个条件的外连接的示例,这些外连接经常被放入 where 条件中,但没有真正完全使用两个条件进行连接。

I have two IEnumerables called BaseReportDefinitions and InputReportDefinitions. I need to do a left outer join where i want all the InputReportDefinitions and whichever BaseReportDefinitions that match. Both IEnumberables contain ReportDefinition objects that contain ParentName and ReportName properties that need to be used as the join key. I want to return the ReportDefinition object for each (in the case of BaseReportDefinition entry it may be null) in an anonymous object.

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.

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

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

发布评论

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

评论(1

十年不长 2024-12-25 15:28:53
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
                                                 });

我相信这最终会成为左外连接。我不知道如何将这个怪物转换为查询语句。我认为如果您在末尾添加 AsQueryable() ,它可以在 Linq-to-SQL 中使用,但说实话,我对此没什么经验。

编辑:我想通了。更容易阅读:

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
                                    };
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.

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