user3300195
user3300195

Reputation: 69

order by to the linq query

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

Answers (3)

Yumei De Armas
Yumei De Armas

Reputation: 396

You could have...

.Take(8).ToList().OrderBy(item => item.vote);

Upvotes: 0

Miles
Miles

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

Andrei
Andrei

Reputation: 56688

.OrderBy(x => x.vote).Take(8).ToList()

Upvotes: 3

Related Questions