简体   繁体   English

如何使用Linq代替foreach进行分组和计数

[英]How to use Linq instead of foreach to Group and Count

I have 2 Lists, one for Players and another for Coaches. 我有2个列表,一个用于播放器,另一个用于教练。

To count number of Players belong to a Coach, I used: 为了计算属于一名教练的球员人数,我使用了:

  var g = AllePlayer.GroupBy(play=> play.coachNumer);
  foreach (var grp in g)
  {
      foreach (Coach co in AlleCoaches)
      {
          if (co.coachNumer== grp.Key)
          {
              co.NumOfPlayer= grp.Count();
          }
       }
  }

Now I want to know if there is a nice way to put the lover parts with "foreach" in a nice Linq syntax to avoid this "foreach" loops. 现在,我想知道是否存在一种很好的方法,可以将带有“ foreach”的情人部分放入一种不错的Linq语法中,以避免这种“ foreach”循环。 Thank you in advance! 先感谢您!

You could change the statement around a little bit. 您可以稍微更改一下语句。 Since ultimately you want to change a property on each coach, it's easiest to loop through that list, as in: 由于最终您想更改每个教练的属性,因此最容易遍历该列表,如下所示:

foreach (Coach co in AlleCoaches)
{
    co.NumOfPlayer= AllePlayer.Where(p => p.coachNumber == co.coachNumber)
                              .Count();
}

This would be a simpler approach: 这将是一个更简单的方法:

var query = AllePlayer.GroupBy(player => player.coachNumer,
                               (coach, players => new {
                                   Coach = coach,
                                   Count = players.Count() }));

That will give you a sequence where each element is the coach and the number of players for that coach. 这将为您提供一个序列,其中每个元素都是教练以及该教练的球员人数。

Then you could iterate over the result, and assign the value into Coach.NumOfPlayer , but do you really need to? 然后,您可以遍历结果,并将值分配给Coach.NumOfPlayer ,但是您真的需要吗? If you do, this will do it: 如果您这样做,则可以这样做:

foreach (var pair in query)
{
    pair.Coach.NumOfPlayer = pair.Count;
}

Personally it doesn't feel like "number of players" should be part of the Coach type to start with... 就个人而言,从“开始”的角度来看,“球员数量”应该不是Coach一部分。

AlleCoaches.ToList()
.ForEach(n=>n.NumOfPlayer=AllePlayer.Where(n=>coachNumer==n.coachNumer).Count());
var g = AllePlayer.TakeWhile(play=> ( play.coachNumer!=null));

您可以采用所需的任何逻辑,但是语法相同。

var count = g.Count();

如何使用 linq 而不是 foreach 来获取列表<object><div id="text_translate"><pre> public List<object> Query() { List<string> availableBillingIncrements = _controller.GetAvailableDropdowns().AvailableBillingIncrement; List<object> results = new List<object>(); var x = availableBillingIncrements.Select(i => new List<object> { "Increment", i }).ToList().Select(i => new List<object> { i }).ToList(); foreach (string increment in availableBillingIncrements) { List<object> result = new List<object>(); result.Add(new List<object> { "Increment", increment }); results.Add(result); } return results; }</pre><p> 在上面的代码中,x 和 results 是相同的对象,除了 x 的类型是 List<List<object>> 并且 results 是 List<object>。 如何摆脱 foreach 循环并使用 x 作为结果?</p></div></object> - how to use linq instead of foreach to get an List<object>

暂无
暂无

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

相关问题 按每个组的计数限制组 Linq - Limit group by count foreach group Linq 如何在C#中使用Linq计数与group by - how to use Linq count with group by in C# 如何在Linq中使用分组和计数 - How to use group by and having count in Linq 如何使用LINQ或Lambda代替嵌套和多个foreach语句 - How to use LINQ or Lambda instead of nested and multiple foreach statements 如何使用 linq 而不是 foreach 来获取列表<object><div id="text_translate"><pre> public List<object> Query() { List<string> availableBillingIncrements = _controller.GetAvailableDropdowns().AvailableBillingIncrement; List<object> results = new List<object>(); var x = availableBillingIncrements.Select(i => new List<object> { "Increment", i }).ToList().Select(i => new List<object> { i }).ToList(); foreach (string increment in availableBillingIncrements) { List<object> result = new List<object>(); result.Add(new List<object> { "Increment", increment }); results.Add(result); } return results; }</pre><p> 在上面的代码中,x 和 results 是相同的对象,除了 x 的类型是 List<List<object>> 并且 results 是 List<object>。 如何摆脱 foreach 循环并使用 x 作为结果?</p></div></object> - how to use linq instead of foreach to get an List<object> 如何在C#中使用linq代替foreach循环 - How to use linq instead of foreach loop in c# 如何使用 linq foreach - how to use linq for foreach 如何在Linq中使用foreach - how to use foreach in linq LINQ计数为空组返回1而不是零 - LINQ Count returning 1 instead of zero for an empty group 如何使用 LINQ 计算最大组中的对象数? - How do I use LINQ to count number of objects in largest group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM