数据视图上的 LINQ

发布于 2024-12-29 23:19:53 字数 1199 浏览 0 评论 0原文

我无法在不进行强制转换的情况下查询 DataView。 IsPresent 方法中的两行代码取自"LINQ - 指定的转换对于数据视图使用无效",并且似乎对每个人都有效,除了一位评论者。我正在“使用”LINQ 命名空间,那么我的问题是什么?

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

    }
}

错误 2“System.Data.DataView”不包含“Any”的定义,并且找不到接受“System.Data.DataView”类型的第一个参数的扩展方法“Any”(您是否缺少 using 指令或程序集引用?) A:\TEMP\ConsoleApplication1\ConsoleApplication1\Program.cs ConsoleApplication1 20 20

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2025-01-05 23:19:53

尝试像在 Main 方法中那样转换表:

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

我也尝试了 .Cast() 就像在 Main 方法中一样,并且也进行了编译。

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.

小嗷兮 2025-01-05 23:19:53

这是因为 DataView 实现了非泛型 IEnumerableLINQ 可以与 IEnumerable 配合使用吗? )。最重要的是,这有点令人困惑,因为 DataView 枚举器返回 DataRowView 类型的对象。

使用 .Cast() Linq 方法,因为您知道它们都是该类型。从技术上讲,您可以使用 .OfType(),但请注意,它会默默地忽略集合中不属于该类型的任何对象,而 Cast 将抛出异常( https://stackoverflow.com/a/4015967/530545 )。

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 ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文