将Foreach循环转换为linq形式

时间:2019-05-21 13:53:45

标签: c# linq

我有Dictionary<string,List<Member> members,并且我有foreach循环。如何隐式循环到linq表达式中?

members.Foreach(x => x.Where(a=>a.Firstaname ?
 
foreach (var key in games.Keys)
{
  foreach (var val in games[key])
  {
    if (firstName == val.FirstName && lastName == val.LastName && command == val.CommandName)
    {
      val.Position = position;
    }
  }
}

如果它与词典中的数据匹配,则需要设置玩家的位置

1 个答案:

答案 0 :(得分:2)

var items = games.SelectMany(x => x.Value).Where(val => firstName == val.FirstName && lastName == val.LastName && command == val.CommandName);
foreach(var i in items)
{
    i.Position = position;
}