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

Sign up to request clarification or add additional context in comments.

1 Comment

it's counting all rows in the dgv...not what I want. Shld be "count rows where BoxAge is between 3 and 5"...
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.

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.