Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to DataGridViewRowCollection

I have a bit of a mystery regarding a LINQ query on a DataGridViewRowCollection. Here is my query (where "grid" is a DataGridView object):

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

I have a project which contains this query and it executes perfectly. The problem is that in another project, I get the following error when I try to build the 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 that it was a reference issue, but I'm using the same references 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 have any idea as to why my LINQ query would work in one project but not the other?

Edit 1:

For the record, here is the exact context where the query is working:

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 just came across the following link...see the accepted answer...this is what I'm trying to do. For some reason I can't get the IEnumerable.Cast() extension method to work...what am I missing?

like image 875
HydroPowerDeveloper Avatar asked Feb 22 '23 13:02

HydroPowerDeveloper


1 Answers

Actually I don't think your code looks exactly like that. Since DataGridViewRowCollection does not implement IEnumerable<DataGridViewRow> your have to use Cast<DataGridViewRow>() like this:

var rows = from row in grid.Rows.Cast<DataGridViewRow>()
           where row.Selected
           select row;
like image 161
Magnus Avatar answered Feb 26 '23 17:02

Magnus