LINQ en un DataView

I cannot query a DataView without casting. The two lines of code in the method IsPresent are taken from "LINQ: la conversión especificada no es válida con el uso de la vista de datos" and seem to work for everyone, except for one commenter. I am "using" the LINQ namespace, so what is my problem?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // works
            DataView o = new DataView();
            var p = from x in o.Cast<DataRowView>() where x.Row.Field<bool>("xxx") select x.Row;
        }

        public static bool IsPresent(DataView dvDataTag, string colName)
        {
            // does not work
            int count = dvDataTag.Count(drv => string.Equals("1", drv[colName].ToString())); 
            return dvDataTag.Any(drv => string.Equals("1", drv[colName].ToString()));
        } 

    }
}

Error 2 'System.Data.DataView' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'System.Data.DataView' could be found (are you missing a using directive or an assembly reference?) A:\TEMP\ConsoleApplication1\ConsoleApplication1\Program.cs ConsoleApplication1 20 20

preguntado el 01 de febrero de 12 a las 03:02

2 Respuestas

try casting the table as you did in the Main method as such:

            int count = dvDataTag.OfType<DataRowView>().Count(drv => string.Equals("1", drv[colName].ToString()));
        return dvDataTag.OfType<DataRowView>().Any(drv => string.Equals("1", drv[colName].ToString()));

También probé .Cast<DataRowView>() as in your Main method, and that also compiled.

Respondido 01 Feb 12, 08:02

As a side comment, I just added .OfType<DataRowView>() to your code. I have no idea how it will behave with real data. - eppdog

Thanks, but others do not need to cast, so I am wondering why I have to cast. I must be missing something. - AMissico

Others also have to cast but do it implicitly e.g. like this "(from DataRowView drv in dvDataTag select drv).Any()" - Samuel

I am not positive, but I think that the need to cast in this case is a safety net put into place to avoid any loss in data that could be incurred if a cast is made implicitly and unconsciously. - eppdog

Eso es porque DataView implementa el no genérico IEnumerable ( ¿LINQ funciona con IEnumerable? ). On top of that, it's a bit more confusing simply because the DataView enumerator returns objects of type DataRowView.

Utiliza .Cast<DataRowView>() Linq method because you know they're all of that type. Technically you can get away with .OfType<DataRowView>(), but be aware that it will silently ignore any objects in the collection that aren't of that type, while Cast lanzará una excepción https://stackoverflow.com/a/4015967/530545 ).

Respondido 02 Oct 20, 17:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.