How to get the count of each unique value occurrences in data table column using linq query?

Hi,
I have a datatable and I need the output as per below. Can some help on the approach to get the solution using Linq query? Thanks in-advance.

Datatable :

Name
Ind
Aus
Spain
US
Spain
Spain
US
Aus
Aus
Ind

Output :
Ind : 2
Aus : 3
Spain : 3
US : 2

Prepare an empty datatatable with build datatable and configure 2 cols, Country, Count - dtResult

Assign Activity:
dtResult =

(From d in YourDataTableVar.AsEnumerable
Group d by k=d(0).ToString.ToUpper.Trim into grp=Group
Let ra = new Object(){k, grp.Count}
Select r = dtResult.Rows.Add(ra)).CopyToDataTAble
1 Like

Hi @Harsh777Dev

Try the below linq expression

(From row In yourDataTableVar.AsEnumerable()
Group row By value = row("YourColumnName") Into Group
Select value, Count = Group.Count).ToList

In the place of yourDatatablevar give the datatable variable and in the yourcolumnname give the column name where you want the values.

Hope it helps!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.