数据视图上的 LINQ
我无法在不进行强制转换的情况下查询 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试像在 Main 方法中那样转换表:
我也尝试了
.Cast()
就像在 Main 方法中一样,并且也进行了编译。Try casting the table as you did in the Main method as such:
I also tried
.Cast<DataRowView>()
as in your Main method, and that also compiled.这是因为
DataView
实现了非泛型IEnumerable
(LINQ 可以与 IEnumerable 配合使用吗? )。最重要的是,这有点令人困惑,因为DataView
枚举器返回DataRowView
类型的对象。使用
.Cast()
Linq 方法,因为您知道它们都是该类型。从技术上讲,您可以使用.OfType()
,但请注意,它会默默地忽略集合中不属于该类型的任何对象,而Cast
将抛出异常( https://stackoverflow.com/a/4015967/530545 )。It's because
DataView
implements the non-genericIEnumerable
( Does LINQ work with IEnumerable? ). On top of that, it's a bit more confusing simply because theDataView
enumerator returns objects of typeDataRowView
.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, whileCast
will throw an exception ( https://stackoverflow.com/a/4015967/530545 ).