删除其他列表中的列表项

时间:2018-06-19 10:45:42

标签: c# linq

我想我已经到了一半,因为我设法选择了我要删除的所有项目。如何在不诉诸foreach循环的情况下实际删除它们?

List<LadderMatchItem> matchResults = (from p in ladder.Participants
                                      from m in p.MatchResults
                                      where m.Id == command.LadderMatchId
                                      select m).ToList();

编辑:简化的类结构

public class Ladder
{       
        public string Id { get; set; }
        public IList<LadderParticipant> Participants { get; set; }
}

public class LadderParticipant
{
    public string Id { get; set; }
    public List<LadderMatchItem> MatchResults { get; set; }
}

public class LadderMatchItem
{
    public string Id{ get; set; }

    public LadderMatchResult Result { get; set; }
}

1 个答案:

答案 0 :(得分:1)

你可以尝试改变你的状况

ladder.Participants.ForEach(x => x.MatchResults.RemoveAll(ele => ele.Id == command.LadderMatchId));