C# .NET - Linq code is not working - Asked By raj kumar on 14-Feb-12 02:21 AM

var result = from GridViewRow msgRow in gvCurUser.Rows
             where ((CheckBox)msgRow.FindControl("chkSelect")).Checked=false
             select gvCurUser.Rows[msgRow.RowIndex];

I want Only checkbox unchecked count in Gridview
Web Star replied to raj kumar on 14-Feb-12 02:27 AM
You are usgn linq in wrong way that's why it did not working
LINQ query should be work on collection not with girdview control filter like you are using .
You need to first get linq query with where clause to filter result collection than you bind that collection with gridview control 

something like this

string[] cities = { "London""Amsterdam""San Francisco""Las Vegas",


              "Helsinki""Nice""Dublin" };

    GridView1.DataSource = from city in cities

                 where city.Length > 4

                 orderby city

                 select city.ToUpper();


Somesh Yadav replied to raj kumar on 14-Feb-12 02:44 AM

here is some code that will represent each DataRow with the Customer Object.

static void Main(string[] args)    
     List<Customer> customers = new List<Customer>();         for(int i=1; i<=10; i++)    
    {          Customer customer = new Customer();          customer.FirstName = "FirstName " + i;          customer.LastName = "LastName " + i;          customers.Add(customer);  
         DataTable customerTable = new DataTable();  
      customerTable.Columns.Add(new DataColumn("FirstName"));  
      customerTable.Columns.Add(new DataColumn("LastName")); 
       foreach(Customer customer in customers)
       DataRow row = customerTable.NewRow();        
              row["FirstName"] = customer.FirstName;       
   row["LastName"] =  customer.LastName;                
second example even you cann do like this also.
string[] Customers = { "sales", "marketing", "Hr", "Finanace",

        "pharmacy", "absgsgs", "kdfjjhkj", "kjieuiu",


    GridView1.DataSource = from customer in customers

         where customer.Length > 4

         orderby customer

         select customer.ToUpper();


hope it helps you.