Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL using group By, and order by count

Tags:

c#

sql

mysql

linq

This is mysql query:

SELECT count(PVersion), PVersion
  FROM [Products].[dbo].[Active_Details] 
group by PVersion 
order by count(PVersion);

What will be its LINQ to SQL.

like image 394
aayushi soni Avatar asked Sep 28 '10 11:09

aayushi soni


2 Answers

Should be a group into:

var product = (
    from p in yourContext.Active_Details
    group p by p.PVersion into pgroup
    select new { VersionCount= pgroup.Count(), pgroup.Key }
).OrderBy(x=>x.VersionCount);

Here is a MSDN Resource with examples

like image 128
Nix Avatar answered Nov 18 '22 01:11

Nix


Try this:

var product = 
            from p in yourContext.Active_Details
            group p by p.PVersion into pgroup
            let count = pgroup.Count()
            orderby count
            select new { Count = count, PVersion = pgroup.Key };

SELECT count(ProductVersion), ProductVersion , ProductID , SubProductID 
FROM [do-not-delete-accounts].[dbo].[Activation_Details] 
group by ProductVersion,ProductID,SubProductID 
order by count(ProductVersion);

var query = 
            from p in yourContext.Activation_Details
            group p by new 
            { 
               ProductVersion = p.ProductVersion, 
               ProductID = p.ProductID,
               SubProductID = p.SubProductID 
            } 
            into pgroup
            let count = pgroup.Count()
            orderby count
            select new 
            { 
                Count = count, 
                ProductVersion = pgroup.Key.ProductVersion, 
                ProductID = pgroup.Key.ProductID,
                SubProductID = pgroup.Key.SubProductID  
            };
like image 18
Bear Monkey Avatar answered Nov 18 '22 00:11

Bear Monkey