views:

2575

answers:

3

I have a DataGridView that I want to query using Linq (C# WinForm). I want to "count" rows where a certain criteria is met. For example,

variable1 = "count rows where ColumnBoxAge > 3 || < 5"

label1.Text = variable1

How to do this in C# WinForm using Linq?

+1  A: 

I don't know if it could work but you can try this;

dataSet.Tables[0].AsEnumerable().Where(c => c.Field<int>("ageColumn") > 3 ||
     c.Field<int>("ageColumn") < 5).Count();

Edit : Where instead of Select.

yapiskan
it's counting all rows in the dgv...not what I want. Shld be "count rows where BoxAge is between 3 and 5"...
MarlonRibunal
Now that's working...(See Edit)
MarlonRibunal
A: 

So your query is wrong! Try to put '&&' instead of '||';

dataSet.Tables[0].AsEnumerable().Where(c => c.Field<int>("ageColumn") > 3 &&
     c.Field<int>("ageColumn") < 5).Count();

Edit : Where instead of Select.

yapiskan
A: 

@yapiskan

dataSet.Tables[0].AsEnumerable().Where(c => c.Field<int>("ageColumn") > 3 &&
     c.Field<int>("ageColumn") < 5).Count();

.Where instead of .Select

Thank you very much! I appreciate your help.

MarlonRibunal
Oops, sure it is!
yapiskan