tags:

views:

91

answers:

4

Hello,

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.

+2  A: 

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

Nix
Thanks for your response, but I am not able to access this
aayushi soni
question asked for order by as well, see my solution.
Bear Monkey
A: 

Thanks for your response. But I am not able to access this p.pVersion in select new braces.

select new { VersionCount= pgroup.Count(), p.PVersion };

aayushi soni
+2  A: 

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  
            };
Bear Monkey
A: 

Thank u very much. One more thing I want that I also want to access the other columns of table like PID, SPID . But this query does not work,

How can I?

aayushi soni
please add a comment instead of adding an answer. Youll have to show me the sql query you have in mind as what you asking may not be possible with aggregate functions.
Bear Monkey
This is MY SQL query: SELECT count(ProductVersion), ProductVersion , ProductID , SubProductIDFROM [do-not-delete-accounts].[dbo].[Activation_Details] group by ProductVersion,ProductID,SubProductID order by count(ProductVersion); what will be its Linq to sql.
aayushi soni
Ive updated my answer with your new question. Please mark it as accepted if this is good for you.
Bear Monkey