1

我该怎么做呢

从 MyTable 中选择前 10 名 Foo

SELECT        TOP (30) Item, Descripcion, SUM(Amount) AS Suma         
FROM            Venat 
GROUP BY Item, Descripcion                          
ORDER BY Suma 

在 Linq 到 SQL 中?

仅按项目分组,而不按描述分组

var filtroprimeros30 = from nuevo in registrosVipDosAños
                     group nuevo by nuevo.Item into g         
                       select new
                        {
                            Item = g.Key,
                            Suma = g.Sum(nuevo => nuevo.Amount)

                        };
4

2 回答 2

1

使用匿名类型进行分组:

var filtroprimeros30 = 
     (from nuevo in registrosVipDosAños
      group nuevo by new { nuevo.Item, nuevo.Description } into g  // here    
      select new {
          g.Key.Item,
          g.Key.Description,
          Suma = g.Sum(n => n.Amount)
      })
     .OrderBy(x => x.Suma)
     .Take(30);

我实际上会这样做(因为查询语法具有很好的分组语法,但没有能力获取 N 项):

var items = from n in registrosVipDosAños
            group n by new { n.Item, n.Description } into g
            select new {
              g.Key.Item,
              g.Key.Description,
              Suma = g.Sum(x => x.Amount)
            };

var topItems = items.OrderBy(x => x.Suma).Take(30);

查询仍然只会执行一次,但现在它更具可读性。

于 2013-03-14T13:09:29.113 回答
1

语法替代

var filtroprimeros30 =  registrosVipDosAnos
                        .GroupBy(m => new {m.Item, m.Description})
                        .Select(g => new {
                           Item = g.Key.Item,
                           Description = g.Key.Description,
                           Suma = g.Sum(n => n.Amount)
                        })
                        .OrderBy(x => x.Suma)
                        .Take(30);
于 2013-03-14T13:14:41.590 回答