将T-SQL转换为LINQ to SQL

时间:2012-12-10 03:20:13

标签: asp.net sql linq-to-sql

我有以下查询,我想转换为LINQ to SQL(c#),但我遇到了困难。

select title, barcode, count (*) as pop_rank
from favourites
group by barcode, title
order by pop_rank desc

我到目前为止

DataContext db = new DataContext();

using (db)
{
var test = from t in db.favourites
           group t by new 
           {
              t.barcode,
              t.title
           };
}

我正在努力通过功能添加计数和订单。

由于

1 个答案:

答案 0 :(得分:1)

尝试:

DataContext db = new DataContext();

using (db)
{
 var test = 
          ( 
           from t in db.favourites
           group t by new 
           {
              t.barcode,
              t.title
            } into g 
             select new {g.Key.barcode, g.Key.title, pop_rank=g.Count()}
            ).OrderBy(a => a.pop_rank);
}