在Linq中對此有一些問題。我有一個記錄列表如下圖所示:使用linq根據不同的第二列從一列中選擇所有值
var allTeams = new List<AllTeamData>()
{
new AllTeamData { Team = "Bengals", Conference = "AFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Steelers", Conference = "AFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Patriots", Conference = "AFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Bills", Conference = "AFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Dolphins", Conference = "AFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Lions", Conference = "NFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Bears", Conference = "NFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Seahawks", Conference = "NFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Cardinals", Conference = "NFC", ConferenceEmail = "[email protected]" },
new AllTeamData { Team = "Vikings", Conference = "NFC", ConferenceEmail = "[email protected]" }
};
什麼,我試圖做的是這種形式建立一個記錄列表:
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();
希望這一切都有道理。我試圖找出是否需要放置一個嵌套查詢來拉動所有團隊並將其放入該對象列表中。不知道如何去做。任何見解都會很棒。
看看如何使用LINQ'GroupBy' – Nkosi