简体   繁体   中英

Query Only the Highest Value of the Votes

//query the list of winners of the said event
    public ActionResult List_of_Winners(int id=0) {
        var winners = (from cat in db.Events_Category_tbl
            join can in db.Candidates_Info_tbl on cat.events_category_id equals can.events_category_id
            where cat.events_info_id == id
            select new Candidates
            {
                events_category_name = cat.events_category_name,
                candidates_fullname = can.candidates_fullname,
                candidates_info_id = can.candidates_info_id,
                events_category_id = cat.events_category_id,
                no_of_votes = can.no_of_votes.Value
            }).Take(1).OrderBy(x=>can.no_of_votes);

        return PartialView(winners);
    }

In this code, Iam planning to get the highest no_of_votes in each category. But, I got an error with this one. How will I query into the database to get the data that I want?

This is the error displayed: "The name 'can' does not exist in the current context"

You would need to move where you have the orderby/take, I believe something like this should work:

var winners = from cat in db.Events_Category_tbl
              from can in
                  (from c in db.Candidates_Info_tbl
                   where c.events_category_id == cat.events_category_id
                   select c).OrderByDescending(c => c.no_of_votes).Take(1)
              where cat.events_info_id == id
              select new Candidates
              {
                  events_category_name = cat.events_category_name,
                  candidates_fullname = can.candidates_fullname,
                  candidates_info_id = can.candidates_info_id,
                  events_category_id = cat.events_category_id,
                  no_of_votes = can.no_of_votes.Value
              };

For ties something like this may work (should return everyone who got the highest score per category):

var winners = from cat in db.Events_Category_tbl
              from max_votes in
                  (from c in db.Candidates_Info_tbl
                   where c.events_category_id == cat.events_category_id
                   select c.no_of_votes).OrderByDescending(c => c).Take(1)
              join can in db.Candidates_Info_tbl on cat.events_category_id equals can.events_category_id
              where cat.events_info_id == id && can.no_of_votes == max_votes
              select new Candidates
              {
                  events_category_name = cat.events_category_name,
                  candidates_fullname = can.candidates_fullname,
                  candidates_info_id = can.candidates_info_id,
                  events_category_id = cat.events_category_id,
                  no_of_votes = can.no_of_votes.Value
              };

It could also be done with GroupBy's or different style inner queries, I imagine, but that 'from x in (from y in z where y.something = x.something select something).Take(n)' pattern results in cross/outer apply in SQL server which seems to perform well in most cases I've seen.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM