Reputation: 69
How to add orderby function in this linq query.I want to display the rows orderby the calculated vote(You can see the calculation inside select query)
(from e in db.FoodItems
join o in db.Ratings
on e.itemid equals o.itemid into g
select new FoodView
{ itemid=e.itemid,
caption=e.caption,
description=e.description,
price=e.price,
pubdate=e.pubdate,
imageurl=e.imageurl,
videourl=e.videourl,
itemname = e.itemname,
vote = (long)Math.Round((double)(g.Sum(x => (int?)x.vote) ?? 0) / g.Count(),0)
}).Take(8).ToList();
Upvotes: 0
Views: 56
Reputation: 396
You could have...
.Take(8).ToList().OrderBy(item => item.vote);
Upvotes: 0
Reputation: 5736
Can you just add it after the take?
(from e in db.FoodItems
join o in db.Ratings
on e.itemid equals o.itemid into g
select new FoodView
{ itemid=e.itemid,
caption=e.caption,
description=e.description,
price=e.price,
pubdate=e.pubdate,
imageurl=e.imageurl,
videourl=e.videourl,
itemname = e.itemname,
vote = (long)Math.Round((double)(g.Sum(x => (int?)x.vote) ?? 0) / g.Count(),0)
}).OrderBy(f => f.vote).Take(8).ToList();
Upvotes: 0