3

I cannot query a DataView without casting. The two lines of code in the method IsPresent are taken from "LINQ - Specified cast is not valid with dataview use" 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

Community
  • 1
  • 1
AMissico
  • 21,470
  • 7
  • 78
  • 106

2 Answers2

2

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()));

I also tried .Cast<DataRowView>() as in your Main method, and that also compiled.

eppdog
  • 423
  • 2
  • 11
  • As a side comment, I just added .OfType() to your code. I have no idea how it will behave with real data. – eppdog Feb 01 '12 at 04:01
  • Thanks, but others do not need to cast, so I am wondering why I have to cast. I must be missing something. – AMissico Feb 01 '12 at 05:03
  • Others also have to cast but do it implicitly e.g. like this "(from DataRowView drv in dvDataTag select drv).Any()" – Samuel Feb 01 '12 at 09:25
  • 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 Feb 01 '12 at 16:47
0

It's because DataView implements the non-generic IEnumerable ( Does LINQ work with IEnumerable? ). On top of that, it's a bit more confusing simply because the DataView enumerator returns objects of type DataRowView.

Use the .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 will throw an exception ( https://stackoverflow.com/a/4015967/530545 ).

Granger
  • 3,639
  • 4
  • 36
  • 34