LINQ to DataGridViewRowCollection

I have a little secret regarding LINQ query in DataGridViewRowCollection. Here is my query (where the "grid" is the DataGridView object):

var rows = from DataGridViewRow row in grid.Rows where row.Selected select row; 

I have a project that contains this request and it runs perfectly. The problem is that in another project, I get the following error when trying to build a solution:

 error CS1936: Could not find an implementation of the query pattern for source type 'System.Windows.Forms.DataGridViewRow'. 'Where' not found. 

At first I thought it was a problem, but I use the same links in both projects:

 using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Data; using System.Data.EntityModel; using System.Drawing; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Diagnostics; 

Does anyone know why my LINQ query will work in one project, but not in another?

Change 1:

For the record, here is the specific context in which the request works. :

 public List<Int64> ComponentIDs { get { return ( from DataGridViewRow row in grid.Rows where row.Selected select (Int64)row.Cells[0].Value ).ToList(); } } 

Edit 2:

I came across the following link ... see accepted answer ... this is what I am trying to do. For some reason, I can't get the IEnumerable.Cast () extension method to work ... what am I missing?

+4
source share
2 answers

Actually, I don’t think your code looks that way. Since the DataGridViewRowCollection does not implement IEnumerable<DataGridViewRow> , you need to use Cast<DataGridViewRow>() as follows:

 var rows = from row in grid.Rows.Cast<DataGridViewRow>() where row.Selected select row; 
+6
source

Try rephrasing the linq query:

 var rows = dgv.Rows .Where(x => x.Selected); 
+1
source

Source: https://habr.com/ru/post/1395420/


All Articles