Click here to Skip to main content
14,775,645 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
                            {
                                kp.TenKP,
                                dmba.TenBenhAn
                            }).GroupBy(p => new
                            {
                                p.TenBenhAn,
                                p.TenKP

                            }).ToList();
                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();
Posted
Updated 2-Nov-20 17:26pm

1 solution

Try:
.GroupBy(
    p => new { p.TenBenhAn, p.TenKP }, 
    (key, items) => new { key.TenBenhAn, key.TenKP, Count = items.Count() })
Enumerable.GroupBy Method (System.Linq) | Microsoft Docs[^]
   
Comments
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 ]
Richard Deeming 3-Nov-20 4:20am
   
That's not an error. That's the result of calling ToString on an object whose class doesn't override the method - it just returns the type name.

List<T> doesn't override ToString, so your sl.ToString() call will simply return the type name: System.Collections.Generic.List`1[System.Boolean]

You need to decide how you want to display the list, and construct a string yourself. For example:
xrSL.Text = string.Join(", ", sl);
Huyyhaha 3-Nov-20 4:37am
   
Thank you it was better!
Huyyhaha 3-Nov-20 4:40am
   
but for I ask them is to are the current counters in each group, doing as any.Cannot in this command is display 'count'
.GroupBy(
p => new { p.TenBenhAn, p.TenKP },
(key, items) => new { key.TenBenhAn, key.TenKP, Count = items.Count() })
Richard Deeming 3-Nov-20 4:44am
   
That code will give you a list of objects containing the two key properties and the number of items. How you display that list is up to you.
Huyyhaha 3-Nov-20 4:45am
   
Thank you very much for your help

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