Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I didn't true use order by in linq

Hi i develop web app with c#. I have sql query and i convert to linq but it’s not working true because of order by

My sql query

Select TOP 3 HastalikIsmi From Hastaliklar group by HastalikIsmi order by Count(*) desc

My linq

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public List<HastalikDto> GetHastalikDto()
    {
        using (SirketDBContext context = new SirketDBContext())
        {
            var result = from hastalik in context.Hastaliklar                            
                         group hastalik by hastalik.HastalikIsmi into isim
                         
                         select new HastalikDto { HastalikIsmi = isim.Key };                         

            return result.OrderBy(h => h.HastalikIsmi).Take(3).ToList();
        }
    }

>Solution :

Here’s how you can do the order by on the count of each group and take the 3 with the highest count.

var result = context.Hastaliklar
    .GroupBy(x => x.HastalikIsmi)
    .OrderByDescending(grp => grp.Count())
    .Select(grp => grp.Key)
    .Take(3)
    .ToList();                       
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading