简体   繁体   English

使用LINQ选择带有其他对象列表的对象

[英]Selecting objects with list of other objects using LINQ

I would like to select objects(A) where one of property is list of objects(B) and based on criteria of object A and object B select only matching. 我想选择对象(A),其中属性之一是对象(B)的列表,并根据对象A和对象B的标准选择仅匹配的对象。

For example: 例如:

class Team //objects A
{
    public string TeamName { get; set; }
    public int StadiumName { get; set; }
    public List<Player> Players { get; set; }
    public string Country { get; set; }
}

class Player //objects B
{
    public string Name { get; set; }
    public int Age { get; set; }
    public float Height { get; set; }
    public float Weight { get; set; }
}

I have teams with some players and I would like to select teams where the country is England and inside that team only Players that are older than 28. For example there are three teams and every team have 16 players. 我的球队中有一些球员,我想选择国家为英格兰的球队,而该球队中只有28岁以上的球员。例如,有三支球队,每支球队有16名球员。 Only two of those teams are from England. 这些队伍中只有两个来自英格兰。 So the result should be list of two teams that have list of player only older than 28 (so list of players may consist of 0 to 16 players) 因此,结果应该是两支球队的名单,这些球队的球员名单仅超过28岁(因此球员名单可能由0到16名球员组成)

I wrote some linq but it either returns team with all players (16 of them) or returns null if there is no player older than 28: 我写了一些linq,但是它返回所有球员(其中16人)的战队,或者如果没有比28岁大的球员返回null。

teams.Where(team => team.Country == "England" && team.Players.Any(player => (player.Age > 28)));

Your requirements essentially necessitate creating a new Team object with a reduced set of players (or alternatively permanently removing the younger players from the original Team object). 实际上,您的要求需要使用减少的玩家数量来创建新的Team对象(或者从原始Team对象中永久删除较年轻的玩家)。

var teamsEnglandOnlyOver28Years =
    teams
    // Only teams from England
    .Where(t => t.Country == "England")
    // Create a new instance with only the older players
    .Select(t => new Team()
    {
        TeamName = t.TeamName,
        StadiumName = t.StadiumName,
        Players = t.Players.Where(p => p.Age > 28).ToList(),
        Country = t.Country       
    });

This may or may not be what you actually intend. 这可能是您实际想要的,也可能不是。 It seems a bit awkward because, for example, the team name is the same as for the original team but the roster is reduced. 似乎有点尴尬,因为例如,团队名称与原始团队相同,但是名册减少了。

Another option is to have a GetPlayers(Func<Player,bool> condition) method which you can use to get filtered players: 另一个选择是拥有一个GetPlayers(Func<Player,bool> condition)方法,该方法可用于获取过滤的播放器:

class Team
{
    public string TeamName { get; set; }
    public int StadiumName { get; set; }
    public List<Player> Players { get; set; }
    public string Country { get; set; }

    public IEnumerable<Player> GetPlayers(Func<Player, bool> condition)
    {
        return Players.Where(condition);
    }
}

This is a better solution than returning a new object with filtered players because filtered players are not getting lost, and you can access them from Players property. 与通过筛选后的播放器返回新对象相比,这是一个更好的解决方案,因为筛选后的播放器不会丢失,您可以从Players属性中访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM