使用linq根据不同的第二列从一列中选择所有值

时间:2016-05-15 23:38:31

标签: c# linq

在Linq遇到一些问题。我有一个记录列表,如下所示:

var allTeams = new List<AllTeamData>()
{
    new AllTeamData { Team = "Bengals", Conference = "AFC", ConferenceEmail = "AFC@NFL.COM" },
    new AllTeamData { Team = "Steelers", Conference = "AFC", ConferenceEmail = "AFC@NFL.COM" },
    new AllTeamData { Team = "Patriots", Conference = "AFC", ConferenceEmail = "AFC@NFL.COM" },
    new AllTeamData { Team = "Bills", Conference = "AFC", ConferenceEmail = "AFC@NFL.COM" },
    new AllTeamData { Team = "Dolphins", Conference = "AFC", ConferenceEmail = "AFC@NFL.COM" },
    new AllTeamData { Team = "Lions", Conference = "NFC", ConferenceEmail = "NFC@NFL.COM" },
    new AllTeamData { Team = "Bears", Conference = "NFC", ConferenceEmail = "NFC@NFL.COM" },
    new AllTeamData { Team = "Seahawks", Conference = "NFC", ConferenceEmail = "NFC@NFL.COM" },
    new AllTeamData { Team = "Cardinals", Conference = "NFC", ConferenceEmail = "NFC@NFL.COM" },
    new AllTeamData { Team = "Vikings", Conference = "NFC", ConferenceEmail = "NFC@NFL.COM" }
};

我试图做的是以这种形式建立一个记录列表:

public class ConferenceData
{
    public string Conference { get; set; }
    public string ConferenceEmail { get; set; }
    public List<string> Teams { get; set; }
}

我尝试做的是向特定会议电子邮件发送电子邮件,其中包含该特定会议中的团队列表。这是我能用我的linq查询得到的:

var conferences = (from t in allTeams
                           select new ConferenceData
                           {
                               Conference = t.Conference,
                               ConferenceEmail = t.ConferenceEmail
                           }).Distinct().ToList();

希望这一切都有道理。我试图弄清楚我是否需​​要整理一个嵌套查询来拉动所有团队并将它们放入该对象列表中。不知道该怎么做。任何见解都会很棒。

2 个答案:

答案 0 :(得分:1)

如果您正在编写SQL查询,那么您将尝试按特定字段对记录进行分组。

同样,LINQ采用GroupBy方法,允许您按会议对数据进行分组:

var conferences = allTeams.GroupBy(t => t.Conference)
                          .Select(grp => new ConferenceData
                                         {
                                             Conference = grp.Key,
                                             ConferenceEmail = grp.First().ConferenceEmail,
                                             Teams = grp.Select(x => x.Team).ToList()
                                         })
                          .ToList();

我已经用方法语法编写了这个,因为这是我更熟悉的内容,但如果您愿意,可以将其翻译为查询语法(类似于您当前的查询)

答案 1 :(得分:0)

使用GroupBygroup clause (C# Reference)

var conferences = (from t in allTeams
                  group t by new { t.Conference, t.ConferenceEmail } into g
                  select new ConferenceData {
                      Conference = g.Key.Conference,
                      ConferenceEmail = g.Key.ConferenceEmail,
                      Teams = g.Select(t => t.Team).ToList()
                  }).Tolist();

使用GroupBy

时也是如此
conferences = allTeams.GroupBy(t => new { t.Conference, t.ConferenceEmail })
               .Select(g => new ConferenceData {
                   Conference = g.Key.Conference,
                   ConferenceEmail = g.Key.ConferenceEmail,
                   Teams = g.Select(t => t.Team).ToList()
               }).Tolist();