首页 > 解决方案 > 完全加入 Linq

问题描述

我是编写 Linq 查询的新手,想编写如下查询。

关于要求的简要信息:

我需要为完全连接到另一个具有扩展数据的表的用户计算不同的组键

TABLE - 1:                                         Table - 2: 
---------------                                    -------------

| Id | GrpKey | prdId | UserId|                    | Id | GrpKey | GrpName  | UserId
| 1  | 123455 | Test1 | 11111                      | 1  | 123455 | GroupOne | 1111
| 2  | 123455 | Test2 | 22222                      | 2  | 551234 | GroupTwo | 1111
| 3  | 123455 | Test3 | 22222                      | 3  | 233344 |GroupThree| 1111
| 4  | 551234 | Test4 | 11111                      | 4  | 278344 |GroupFour | 1111
| 5  | 551234 | Test5 | 11111
| 6  | DBNULL | Test4 | 11111
| 7  | DBNULL | Test5 | 11111

REQD. RESULT for UserId : 11111 
--------------------------------

GrpKey | GrpName    | Count(GrpKey)
DBNULL | DBNULL     |  2
551234 | GroupTwo   |  2
123455 | GroupOne   |  1
233344 | GroupThree |  0
278344 | GroupFour  |  0

Queries Tried:

**LEFT JOIN:**
                          from item in table1
                          join grp in table2 on item.GrpKey equals grp.GrpKey  into j1
                          from rt in j1.DefaultIfEmpty()
                          where item.UserId == "1111"
                          group rt by rt.GrpKey into g
                          select new Group
                          {
                                UserId = grp.userId
                                Count = j1.Count(),
                                Name = grp.GrpName,
                                Key = grp.GrpKey,
                          }).ToList();

**RIGHT JOIN:**
              from grp in table2
                          join item in table1 on grp.GrpKey equals item.GrpKey  into j1                          
                          where grp.UserId == "1111"
                          group grp by grp.GrpKey into g
                          select new Group
                          {
                                UserId = grp.userId
                                Count = j1.Count(),
                                Name = grp.GrpName,
                                Key = grp.GrpKey,
                          }).ToList();

result = LeftJoinResult.Union(RightJoinResult).ToList();

TriedQuery1 的问题:

使用上面的 LINQ 查询,我得到的结果集是:

GrpKey | GrpName    | Count(GrpKey)
DBNULL | DBNULL     |  2
551234 | GroupTwo   |  2
123455 | GroupOne   |  1
551234 | GroupTwo   |  1
233344 | GroupThree |  1
278344 | GroupFour  |  1

请帮助我如何将此左连接转换为完全连接或数据,如 Reqd 结果

提前致谢

标签: c#.netentity-frameworklinq

解决方案


所以你有一个序列Table2Elements,其中每个Table2Element都至少有一个 GrpName、一个 GrpKey 和一个 UserId。

此外,您有一个序列Table1Elements,其中每个Table1Element也有一个 GrpKey 和一个 UserId。

要求:对于每个 Table2Element,给我 GrpKey、GrpName 和 Table1Element 的数量,这些 Table1Element 的 [GrpKey, UserId] 值与 Table2Element 的值相同。

每当您有一系列项目 A 时,其中每个项目 A 都有另一个表中属于该项目 A 的一些项目 B,例如学校及其学生、作者及其书籍、客户及其订单,请考虑使用 Enumerable.GroupJoin,或者因为您使用的是实体框架Queryable.GroupJoin

var result = dbContext.Table2          // GroupJoin the elements of Table2
.GroupJoin(dbContext.Table1,           // with the elements of Table1
table2Element => new                   // from every element of Table2 take properties ...
{
    GrpKey = table2Element.GrpKey,
    UserId = table2Element.UserId,
},
table1Element => new                   // from every element of Table1 take properties ...
{
    GrpKey = table1Element.GrpKey,
    UserId = table1Element.UserId,
},

// parameter resultSelector: take the element of Table2,
// with all zero or more elements of Table1 with the same key
// to make one new resulting element:
(table2Element, table1ElementsWithSameKey) => new
{
    GrpKey = table2Element.GrpKey,
    GrpName = table2Element.GrpName,

    Count = table1ElementsWithSameKey.Count(),
});

简单的来吧您好!


推荐阅读