使用 linq 将 IGrouping 从一个类转换为另一个类
我有两个类 Teams 和 PlayerTeams
public class PlayerTeams
{
public int ID { get; set; }
public string PlayerName { get; set; }
public string PlayerCountry { get; set; }
public bool IsActive { get; set; }
public string PlayerTeam { get; set; }
}
public class Players
{
public int ID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public bool? Status { get; set; }
}
我有一个 PlayerTeams
列表,它按 PlayerTeam
分组,如下所示。
var groupedPlayers = teams
.OrderBy(x => x.PlayerName)
.GroupBy( x => x.PlayerTeam)
.ToList();
它的类型为 List
但我希望它的类型为 List
因为我不想要每行的冗余关键信息。
我怎么可能做到这一点?我只能想到 IGrouping
上的 .ConvertAll()
之类的东西。我也做不到。
有没有有效的方法来做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以更改分组,我只需使用:
其中
Players.FromPlayerTeam
是Players
中的静态方法,它接受PlayerTeam
并返回一个玩家
。此外,我建议使用
ToLookup
而不是GroupBy
-Lookup
正是您想要的,而不必担心ToList调用。
If you can change the grouping, I'd just use:
Where
Players.FromPlayerTeam
is a static method inPlayers
which takes aPlayerTeam
and returns aPlayers
.Additionally, I'd suggest using
ToLookup
instead ofGroupBy
- aLookup
is exactly what you want here, without bothering with theToList
call.这不是测试集,只是一个想法。
如果您在将需要
IGrouping
类型的 linq 语句转换为字符串列表时遇到困难,那么您可能必须 >之前选择它。This not testet, just an idea.
If you have trouble converting your linq statement, which is expecting the
IGrouping
type, to a string list, then you might have to select it before.