亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Linq 過濾器以避免循環

Linq 過濾器以避免循環

C#
海綿寶寶撒 2023-07-09 10:28:45
我正在嘗試制定 LINQ 查詢,但不是專業人士,所以無論我嘗試什么都不起作用。我想過濾列表以從下面的列表中獲取電子郵件 ID,其中分數為 0,并按團隊名稱分組。我嘗試這樣做的方式是:獲取不同團隊名稱的列表。循環遍歷每個不同的團隊名稱并獲取分數為 0 的電子郵件 ID。Team    Name   Score    EmailId   Hawk    Amy     0       [email protected]   Hawk    Aby     0       [email protected]   Hawk    Raf     1       [email protected]   Hawk    Jay     2       [email protected]   Eagle   Blu     0       [email protected]   Eagle   Tru     1       [email protected]我想得到兩行:Hawkand [email protected], [email protected],下一個結果是Eaglewith [email protected]。 這可以通過 LINQ 一步完成嗎?
查看完整描述

3 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

不確定你現在在做什么,但這就是我會做的

var result = list.Where(p => p.Score == 0)
                 .Select(p => new{p.Team, p.EmailId})
                 .GroupBy(p => p.Team)
                 .Distinct();


查看完整回答
反對 回復 2023-07-09
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

想要過濾列表以獲取分數為 0 且按團隊名稱分組的電子郵件 ID。

過濾列表以獲取分數為 0 的電子郵件 ID

var filteredList = list.Where(record => records.Score == 0);

按團隊名稱分組

var groupedByTeamName = filteredList.GroupBy(record => record.Team)

IEnumerable<IGrouping<TRecord, TTeam>>如果我沒記錯的話,這將返回一個。IGrouping<T,K>只是一個列表,其中包含Key包含您分組依據的屬性(在本例中為團隊)。

您當然可以以級聯方式調用它們:

list.Where(record => records.Score == 0).GroupBy(record => record.Team);

但是調試會有點困難,因為您必須選擇代碼并快速監視句子的部分。有時這不起作用。


查看完整回答
反對 回復 2023-07-09
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

想要過濾列表以獲取分數為 0 且按團隊名稱分組的電子郵件 ID。

這是一個很難說的方式嗎:

我想要未得分團隊成員的所有電子郵件嗎?

將您的數據分組為“球隊及其球員和得分”;僅保留那些得分為零的球隊并提取球員的電子郵件。

為此,我們使用帶有 aKeySelector 和 aResultSelector 的 GroupBy 重載

var emailsOfPlayersInTeamsWithZeroScor = myInput.GroupBy


? ? // keySelector: the team

? ? .GroupBy(inputItem => inputItem.Team,


? ? // ResultSelector: from every Team with its players and scores

? ? // make sequences of emails of players and check if there is a score at all

? ? (team, players) => new

? ? {

? ? ? ? // not interested in the team


? ? ? ? // remember if all scores are zero

? ? ? ? HasOnlyZeroScore = players.All(player.Score == 0),


? ? ? ? // remember all emails of the players in the team

? ? ? ? PlayerEmails = players.Select(player => player.Email),

? ? })


? ? // keep only the items with a zero score

? ? .Where(team => team.HasOnlyZeroScore)


? ? // and select the lists of emails per team as one big list:

? ? .SelectMany(team => team.PlayerEmails);


查看完整回答
反對 回復 2023-07-09
  • 3 回答
  • 0 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號