正如标题中所提到的,我想使用另一个表的列来h
结果表。
orderby
以下是SQL Equivalent语句。
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();
我得到了理想的结果。但是我怎么能用LINQ来做呢?
答案 0 :(得分:0)
只需创建一个字典,其中键是Product,值是计算Quantity,然后执行查询。
或者这样做:
var product = p in resultOfTheJoin
let count = resultOfTheJoin.Quantity.Count()
orderby count
select p;
答案 1 :(得分:0)
如果我理解,你需要这样的。请试试这个:
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();