var query = (from order in db.OrderDetails
where order.ProductID != 1
join product in db.Products on order.ProductID equals product.ProductID
group product by product.ProductName into result
let count = result.Count()
orderby count descending
select new { ProductName = result.Key, CountOfProducts = count }
).Take(5);
Still not really sure what the
OrderID IN (SELECT ....)
section accomplishes but you can do that with something like this:
db.OrderDetails.Any(o => o.ProductID == 1 && o.OrderID == external.OrderID)
I'll leave that up to you since I honestly can't get the query to do anything useful when including that check. Obviously I'm missing some relationship here.