Click here to Skip to main content
15,564,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a data table
khoa -------- tenban
LCK        bataimuihong 
LCK        bank 
LCK        bank 
LCK        bank
KHNGOAI    bank
KHNGOAI    bataimuihong
KHNGOAI    bataimuihong

how can i count the number of occurrences after grouped by 'khoa' and 'tenban'

What I have tried:

<pre>var data = (from a in _en.EMR_BenhAn.Where(p => p.NgayLap >= tungay && p.NgayLap <= denngay).ToList()
                            join dmba in _en.DM_BenhAn on a.KyHieuBA equals dmba.KyHieuBA
                            join kh in _en.EMR_Khoa on a.MaBAn equals kh.MaBAn
                            join kp in _en.KPhongs on kh.MaKPnh equals kp.MaKP
                            select new
                            }).GroupBy(p => new

                var data2 = data.Select(p=> new
                        TenBenhAn = p.Key.TenBenhAn,
                        TenKP = p.Key.TenKP
                    }).OrderBy(p=> p.TenKP).ToList();

                var sl = data.Select(p => p.Key.TenBenhAn);
                xrSL.Text = sl.ToString();
Updated 2-Nov-20 17:26pm

1 solution

    p => new { p.TenBenhAn, p.TenKP }, 
    (key, items) => new { key.TenBenhAn, key.TenKP, Count = items.Count() })
Enumerable.GroupBy Method (System.Linq) | Microsoft Docs[^]
Share this answer
Huyyhaha 2-Nov-20 23:06pm    
I can view how it came out, Currently I am viewing it like this but it has a 'string' error
Richard Deeming 3-Nov-20 4:07am    
If you want someone to help you fix an error, you need to provide the full details of the error.
Huyyhaha 3-Nov-20 4:09am    
I'm getting the display error when I use this command !
var sl = data.Where(p => p.TenBenhAn.Count() > 0).ToList();

xrSL.Text = sl.ToString();
Richard Deeming 3-Nov-20 4:10am    
Once again: If you want someone to help you fix an error, YOU NEED TO EXPLAIN WHAT THE ERROR IS!
Huyyhaha 3-Nov-20 4:15am    
I'm sorry my network has a problem! I don't know how to explain it! But when I run it it shows this error
system.collections.generic.list 1[ System.boolean ]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900