1

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?

2
  • What do you have as a DataSource in your DataGridView?
    – Ali Ersöz
    Commented Nov 21, 2008 at 7:33
  • i have a dataset from a SQL Server stored proc Commented Nov 21, 2008 at 7:39

3 Answers 3

2

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.

1
  • it's counting all rows in the dgv...not what I want. Shld be "count rows where BoxAge is between 3 and 5"... Commented Nov 21, 2008 at 8:01
0

@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.

0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.