As mentioned in the Title, I want to orderby
the result table using column of another table.
var products = (from p in db.Products
join o in db.OrderDetails on p.ProductID equals o.ProductID
orderby count(o.Quantity) <-- want to do something like this
select p).Distinct().Take(8).ToList();
Below is the SQL Equivalent statement.
SELECT Products.ProductID FROM Products
INER JOIN OrderDetail on OrderDetail.ProductID = Products.ProductID
GROUP BY Products.ProductID
ORDER BY COUNT(OrderDetail.Quantity)
And I am getting desired results. But how can I do it using LINQ?
Just create a dictionary where the key is Product and value is calculated Quantity, and after that perform your query.
Or do it like this way:
var product = p in resultOfTheJoin
let count = resultOfTheJoin.Quantity.Count()
orderby count
select p;
If i understand, you need a just like that. Pls try this:
var query = (from p in db.Products
join o in db.OrderDetails on p.ProductID equals o.ProductID into orderTemp
select new type
{
Product_id = p.Product_id,
//set product attribtes,
OrderDetail_id = orderTemp.OrderDetail_id,
Quantity = orderTemp.Quantity,
// set OrderDetail attributtes
}.OrderBy(l => l.Quantity.Count()).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.