user7951164
user7951164

Reputation:

Distinct Count in lambda expression giving one extra count

I am trying to get distinct count of values from a column
Below is the code I am using

int NewDivision = dt
  .AsEnumerable()
  .Select(i => i.Field<string>("ParentName"))
  .Distinct()
  .Count();

dt is a DataTable here and it has 9 distinct values in it , but it shows count as 10 I have checked the data manually by importing it into Excel sheet , Take a look here enter image description here

Is it also counting blank as a value ?
Additionally when I select only Blank , there is not a single row in data

Upvotes: 0

Views: 609

Answers (1)

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

If the problem is the case then you can use Distinct with StringComparer.CurrentCultureIgnoreCase:

int NewDivision = dt
  .AsEnumerable()
  .Select(i => i.Field<string>("ParentName"))
  .Distinct(StringComparer.CurrentCultureIgnoreCase)
  .Count();

Upvotes: 3

Related Questions