tags:

views:

220

answers:

1

hi how to convert this sql query to linq query? in asp.net

 SELECT  distinct  P.Pid,P.Name,PC.categoryName, 
(select count(id) from POrderdetails where id=p.Pid)as detailsCount
FROM PTable P INNER JOIN PCATEGORY PC ON PC.pcategoryId=P.pCategoryId
ORDER BY detailsCount desc
+1  A: 

Based on your comment, I've updated this to include the category name. Note that the query selects them into an anonymous object -- you can change that if you have an actual class with the properties by using the class constructor with property assignment instead. Note that I also gave the count a different name. As you already have a Pid, I thought pid, would be too confusing.

 var products = PTable.Select( p => new {
                                 Pid = p.Pid,
                                 Name = p.Name,
                                 DetailCount = p.Orderdetails.Count(),
                                 Category = p.PCATEGORY.CategoryName
                        });
tvanfosson
sorry i need to take category name also..
Avinash
I've updated based on your comment.
tvanfosson