-2

i have this query in SQL Server

select max(win_votos), win.cdt_id, cdt_nome 
   from tb_ganhadores_win as win
   inner join tb_candidatos_cdt as cdt on cdt.cdt_id = win.cdt_id
   group by win.cdt_id,cdt_nome

<--

and i need to create a list 'AspNetMvc' page, like

public ActionResult ListWinners(int? id){

LINQ QUERY HERE

return View('that list');

}

sorry about my english

can anyone help me please?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Leonardo
  • 1
  • 4
  • are you using Entity Framework and set it up? – Ehsan Sajjad Jul 30 '18 at 14:18
  • 1
    In my opinion the question is too broad for Stackoverflow. Try to go on line and follow basic tutorial. Then if you have issues, come back and show us the code you've tried. – derloopkat Jul 30 '18 at 14:23
  • i have some things that i forgot to say, in the result of query in sql, i have some itens with same max value like Name 1 - 2.0, Name 2 - 2.0 (can u understand what i trying to say?) – Leonardo Jul 30 '18 at 14:28
  • and this is what i can do: db.Ganhadores .Join(db.Candidatos, ganhadores => ganhadores.cdt_id, candidato => candidato.cdt_id, (ganhadores, candidato) => new { ganhadores, candidato }) .Where(g => g.ganhadores.vot_id == id) .Max(g => g.ganhadores.win_votos); but with this i cant put '.tolist()' – Leonardo Jul 30 '18 at 14:29
  • 1
    Possible duplicate of [How do I get the MAX row with a GROUP BY in LINQ query?](https://stackoverflow.com/questions/157786/how-do-i-get-the-max-row-with-a-group-by-in-linq-query) – Igor Jul 30 '18 at 14:39
  • I will reformulate my doubt, I have a mini voting system, where when closing the voting, show me the winner. But there may be a tie and I need to be shown how many and which names are tied, I think now it has become clearer right? – Leonardo Jul 30 '18 at 14:46

2 Answers2

0

If you use entity framework and your model name is like this you can use some think like this:

var result =  from win in tb_ganhadores_win
              join cdt in tb_candidatos_cdt on cdt.cdt_id = win.cdt_id
              group win by new { win.cdt_id, cdt_nome } into g
                select new
                {
                    max = g.Max(win_votos),
                    win.cdt_id,
                    cdt_nome
                };

and return result.ToList();

note :

"tb_ganhadores_win"

and

"tb_candidatos_cdt"

is your models.Replace it by what you want

Amirhossein Yari
  • 2,054
  • 3
  • 26
  • 38
  • var result = from win in db.Ganhadores join cdt in db.Candidatos on win.cdt_id equals cdt.cdt_id group win by new{ win.cdt_id, cdt.cdt_nome} into g select new { max = g.Max(win.win_votos), win.cdt_id, cdt.cdt_nome }; so i did this, but he is saying that these fields (win cdt inside of select new) do not exist in this context – Leonardo Jul 30 '18 at 14:45
0
var res= (from win in dbContext.tb_ganhadores_wins
          join cdt in dbContext.tb_candidatos_cdts
          on win.cdt_id equals cdt.cdt_id
          select new {win,cdt})
          .GroupBy(x=>new{Id=x.win.cdt_id,Nome=x.cdt.cdt_nome})
          .Select(x=>new
                     {
                       Votos=x.Max(z=>z.win.win_votos),
                       Id=x.Key.Id,
                       Nome=x.Key.Nome
                     })
         .ToList();
Sumit raj
  • 821
  • 1
  • 7
  • 14