LINQ to SQL:通过求和运算进行排序

| 我有一张表,我需要在该表中根据group by子句对数字列求和来对结果集进行排序。 例如,该表具有销售员ID,订单号和订单值。我想要按累计订单价值列出前10名销售人员。 SQL和LINQ的新手,所以任何建议都将不胜感激!     
已邀请:
        
var results = context.Sales.GroupBy(s => s.SalesPersonID)
                           .Select(g => new {
                                              SalesPersonID = g.Key, 
                                              Sales = g.Sum(s => s.OrderValue) 
                                             })
                           .OrderByDescending(r => r.Sales)
                           .Take(10);
    
        像这样:
var orders = new[]
{
    new Order { SalesPersonId = 1, OrderNumber = 1, OrderValue = 1 },
    new Order { SalesPersonId = 1, OrderNumber = 2, OrderValue = 2 },
    new Order { SalesPersonId = 2, OrderNumber = 2, OrderValue = 2 },
};

var topSalesMen = orders
    .GroupBy(arg => arg.SalesPersonId)
    .Select(arg => new { SalesPersonId = arg.Key, TotalOrderValue = arg.Sum(x => x.OrderValue) })
    .OrderByDescending(arg => arg.TotalOrderValue)
    .Take(10)
    .ToList();
    

要回复问题请先登录注册