如何将跨表的外连接表达为单个 Linq 表达式

发布于 2024-09-29 04:33:38 字数 1121 浏览 5 评论 0原文

以下情况: 存在一个充当交叉表(mergeSet)的数组。另外还有 存在一组目标值和一组源值。源值 可以通过交叉表与目标值连接。 -但这怎么可能 通过使用来表达目标集与其他表的外连接 只有一个 LINQ 表达式?

目前我只找到使用多个 LINQ 的解决方案 表达式(即 targetSet+mergeSet+sourceSet 的内部联接,然后是纯 左外部部分,最后是innerJoinResult 和outerPart 的串联)。

        var mergeSet = new[] 
        {
            new KeyValuePair<int, int>(3, 4), 
            new KeyValuePair<int, int>(5, 6)
        };

        var targetSet = new[] { 1, 3, 5, 7 };
        var sourceSet = new[] { 4, 6 };

        var innerJoinResult =
          from mergeItem in mergeSet
          join sourceItem in sourceSet on mergeItem.Value equals sourceItem
          join targetItem in targetSet on mergeItem.Key equals targetItem
          group sourceItem by targetItem;

        var outerPart =
            from targetItem in targetSet
            where !mergeSet.Any(mergeItem => mergeItem.Key == targetItem)
            group 0 by targetItem; // Fill the right part with zero.

        var outerJoinResult = outerPart.Concat(innerJoinResult);

Following situation:
There exists an array acting as crosstable (mergeSet). Additionally there
exists a set of target values and a set of source values. The source values
can be joined with the target values via the crosstable. - But how is it possible
to express an outer join of, e.g., the targetSet against the other tables by using
only one LINQ expression?

Currently I found only solutions using multiple LINQ
expressions (i.e. an inner join of targetSet+mergeSet+sourceSet, then the pure
left outer part and finally a concatenation of the innerJoinResult and the outerPart).

        var mergeSet = new[] 
        {
            new KeyValuePair<int, int>(3, 4), 
            new KeyValuePair<int, int>(5, 6)
        };

        var targetSet = new[] { 1, 3, 5, 7 };
        var sourceSet = new[] { 4, 6 };

        var innerJoinResult =
          from mergeItem in mergeSet
          join sourceItem in sourceSet on mergeItem.Value equals sourceItem
          join targetItem in targetSet on mergeItem.Key equals targetItem
          group sourceItem by targetItem;

        var outerPart =
            from targetItem in targetSet
            where !mergeSet.Any(mergeItem => mergeItem.Key == targetItem)
            group 0 by targetItem; // Fill the right part with zero.

        var outerJoinResult = outerPart.Concat(innerJoinResult);

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

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

发布评论

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

评论(1

真心难拥有 2024-10-06 04:33:38

如下代码:

    var outerJoinResult =
        (from ti1 in targetSet
         join mi1 in mergeSet on ti1 equals mi1.Key into joinedMerge
         from jm1 in joinedMerge.DefaultIfEmpty()
         join si1 in sourceSet on jm1.Value equals si1 into joinedSource
         from js1 in joinedSource.DefaultIfEmpty()
         select new { target = ti1, source = js1 }).ToList();

    outerJoinResult.ForEach(x => Console.Out.WriteLine("Source: {0}, Target: {1}", x.source, x.target));

打印:

Source: 0, Target: 1
Source: 4, Target: 3
Source: 6, Target: 5
Source: 0, Target: 7

The following code:

    var outerJoinResult =
        (from ti1 in targetSet
         join mi1 in mergeSet on ti1 equals mi1.Key into joinedMerge
         from jm1 in joinedMerge.DefaultIfEmpty()
         join si1 in sourceSet on jm1.Value equals si1 into joinedSource
         from js1 in joinedSource.DefaultIfEmpty()
         select new { target = ti1, source = js1 }).ToList();

    outerJoinResult.ForEach(x => Console.Out.WriteLine("Source: {0}, Target: {1}", x.source, x.target));

printed:

Source: 0, Target: 1
Source: 4, Target: 3
Source: 6, Target: 5
Source: 0, Target: 7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文