I am trying to find count of a column values based on other column conditions, basically something similar to countif. e.g.
if I have a data table “Dt1”
C1 C2 C3
a y 3
a n 3
a y 3
a y 3
b y 2
b y 2
d n 0
So i want count of Column1 in Column3 based on the condition that column2 is “y”
A datatable with first two column would be the input, and the output would a datatable with additional column C3, having count of data in C1, based on condition in C2 so I/P would be
C1 C2
a y
a n
a y
a y
b y
b y
d n
and O/P will be
C1 C2 C3
a y 3
a n 3
a y 3
a y 3
b y 2
b y 2
d n 0
for example your condition is how many times ‘a’ appear in column 1
then your condition would be
DtData.Select("ColumnA = 'a'").Count
count function will give you the count of the values. similarly you can apply your condition and with count function get the count of the values on any column.
we can calculate by calculate by group by or by filtering check
orig data - dtData
prepare an empty datatable with buld datatatable having the 3 cols C1,C2,C3 - dtResult
Assign activity
Left: dtResult
Right:
(From d in dtData
Group d by k=d(“C1”).toString.Trim into grp=Group
Let c = grp.Where(Function (x) x(“C2”).toString.Trim.ToUpper.Equals(“Y”)).Count
From g in grp
Let ra = g.ItemArray.Append(c).toArray
Select r = dtResult.Rows.Add(ra)).CopyToDataTable
I tried using this code, but there is some error in this, I get an compiler error, “end of expression expected”, would you please have a look at it. thanks
thanks you very much, It worked very well, exactly what I wanted. BTW what are these codes, which language are these written in, where to learn this from.