C# - 按属性值对对象进行分组

Gar*_*ary 1 c# linq

我有一个用 C# 编写的控制台应用程序。在我的应用程序中,我有一个这样定义的对象:

public class Order
{
  public double Price { get; set; }

  public string Description { get; set; }

  public int ItemCount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有Order一个List<Order>. 我需要按价格对这些订单进行分组。但是,这些组代表了一个价格范围。例如,我想要以下组:

$0 - $10
$10 - $20
$20 - $30
$30 - $40
$40 - $50
$50 - $60
$60 - $70
$70 - $80
$80 - $90
$90 - $100
Run Code Online (Sandbox Code Playgroud)

最初,我只想遍历List<Order>,但它看起来很笨拙。我觉得这对 Linq 来说是一个很好的案例,但我做得不够。我的方法不起作用。

有没有办法根据PriceLINQ的属性将订单分组为 10 美元的增量?

Tot*_*oto 5

这应该工作: var groups = orders.GroupBy(order => order.Price - order.Price % 10);