luke2012
luke2012

Reputation: 1734

Convert T-SQL to LINQ to SQL

I have the following query that I would like to convert to LINQ to SQL (c#) but am getting stuck.

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

I got as far as

DataContext db = new DataContext();

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

I'm struggling with adding the count and order by functions.

Thanks

Upvotes: 1

Views: 88

Answers (1)

Chandu
Chandu

Reputation: 82913

Try:

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);
}

Upvotes: 1

Related Questions