基于多列的Join LINQ查询的分组/排序输出

时间:2019-06-03 19:27:04

标签: linq join group-by

我正在尝试按组输出LINQ查询的结果。所有列表项都是字符串。要加入两个列表的字符串是AssociatedCase。这两个列表是:

  1. InternalIssue-{Id, Key, ReadMe, AssociatedCase}
{"44", "INT-44", "this is the read me for 44", "1234"}
{"54", "INT-54", "this is the read me for 54", "1234"}
{"54", "INT-54", "this is the read me for 54", "5678"}
{"55", "INT-55", null, "9999"}
  1. ExternalCase-{CaseName, Account, Contact, AssociatedCase}
{"EXC-222", "1234", "Nike", "Nancy"}
{"EXC-111", "5678", "Reebok", "Amber"}
{"EXC-000", "9999", "Puma", "Susan"}

我尝试了类似文章中的建议,但是无法使其正常工作-通常,当我开始尝试将列表分组并合并在一起时,某些列表项将变得无法访问。

var query = issueList.Join(caseList,
i => i.AssociatedCase,
c => c.AssociatedCase,
(i, c) => new
{
i.Key,
i.ReadMe,
c.CaseName,
c.Account,
c.Contact
});

foreach (var issue in query)
{
Console.WriteLine($"{issue.Key} - {issue.ReadMe}\n" +
$"\t{issue.CaseName} - {issue.Account}, {issue.Contact}");
}

这就是我想要的:

(INT-44) - this is the read me for 44
   (EXC-222) - Nike, Nancy
(INT-54) - this is the read me for 54
   (EXC-111) - Reebok, Amber
   (EXC-222) - Nike, Nancy
(INT-55) -
   (EXC-000) - Puma, Susan

...但这就是我得到的:

(INT-44) - this is the read me for 44
   (EXC-222) - Nike, Nancy
(INT-54) - this is the read me for 54
   (EXC-222) - Nike, Nancy
(INT-54) - this is the read me for 54
   (EXC-111) - Reebok, Amber
(INT-55) -
   (EXC-000) - Puma, Susan

1 个答案:

答案 0 :(得分:0)

加入后需要分组数据:

var issueList = new[]
{
    new {Id = "44", Key = "INT-44", ReadMe = "this is the read me for 44", AssociatedCase = "1234"},
    new {Id = "54", Key = "INT-54", ReadMe = "this is the read me for 54", AssociatedCase = "1234"},
    new {Id = "54", Key = "INT-54", ReadMe = "this is the read me for 54", AssociatedCase = "5678"},
    new {Id = "55", Key = "INT-55", ReadMe = (string) null, AssociatedCase = "9999"}
};

var caseList = new[]
{
    new {CaseName = "EXC-222", AssociatedCase = "1234", Account = "Nike", Contact = "Nancy"},
    new {CaseName = "EXC-111", AssociatedCase = "5678", Account = "Reebok", Contact = "Amber"},
    new {CaseName = "EXC-000", AssociatedCase = "9999", Account = "Puma", Contact = "Susan"}
};

var query = issueList
    .Join(caseList,
        o => o.AssociatedCase,
        i => i.AssociatedCase,
        (issue, @case) => (issue, @case))
    .GroupBy(v => v.issue.Key)
    .Select(v => new
    {
        v.Key,
        v.First().issue.ReadMe,
        cases = v
            .Select(i => new
            {
                i.@case.CaseName,
                i.@case.Account,
                i.@case.Contact
            })
            .OrderBy(c => c.CaseName)
            .ToArray()
    })
    .ToArray();

foreach (var item in query)
{
    Console.WriteLine($"{item.Key} - {item.ReadMe}");

    foreach (var @case in item.cases)
    {
        Console.WriteLine($"\t{@case.CaseName} - {@case.Account}, {@case.Contact}");
    }
}

/*
result:

INT-44 - this is the read me for 44
        EXC-222 - Nike, Nancy
INT-54 - this is the read me for 54
        EXC-111 - Reebok, Amber
        EXC-222 - Nike, Nancy
INT-55 -
        EXC-000 - Puma, Susan
*/