转换LINQ表达式将引发“内部.NET Framework数据提供程序错误1025". [英] Casting LINQ expression throws "Internal .NET Framework Data Provider error 1025."

查看:100
本文介绍了转换LINQ表达式将引发“内部.NET Framework数据提供程序错误1025".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

model.Items = dbQuery.Select(c => new Item {
    Total = c.Items.Count((Func<Item, bool>)(t => t.Amount.HasValue))
}).ToArray();

引发以下异常:

内部.NET Framework数据提供程序错误1025.

但是确切的代码减去强制转换有效:

model.Items = dbQuery.Select(c => new Item {
    Total = c.Items.Count(t => t.Amount.HasValue)
}).ToArray();

注意:dbQuery只是EF ObjectQuery.

Count()的预期类型是Func<Item, bool>,并且甚至通过ReSharper验证了它们是相同的类型,因为它使转换为灰色,这表明它是不需要的,并且在功能上是等效的.

但是,该异常证明它们不是 ,实际上在功能上是等效的.

此外,我的确看到了解决方案

您要将[the]表达式存储在变量中,以备重用,然后将其传递到Count()".如果Expression可以转换为SQL,则这是100%可能的.这里的问题是您要引入的东西无法转换为SQL.在这种情况下,特别的问题是强制类型转换被转换为Expression.Convert.您的类型绝对不能用SQL表示,因此将无法执行.将两者都插入LINQPad并检查IL以查看区别.

正如您链接到的其他相关问题/答案所建议的那样,您需要将谓词作为Expression而不是Func(如何可以完成工作的代码中的位置).如您所指出的,问题是Count方法没有采用Expression,但是可以通过使用AsQueryable()轻松解决,这将为您提供采用ExpressionIQueryable扩展方法. /p>

这应该没有问题执行

Expression<Func<Item,bool>> predicate = i => i.Amount.HasValue;

model.Items = dbQuery.Select(c => new Item {
    Total = c.Items.AsQueryable().Count(predicate)
}).ToArray();

The following code:

model.Items = dbQuery.Select(c => new Item {
    Total = c.Items.Count((Func<Item, bool>)(t => t.Amount.HasValue))
}).ToArray();

Throws the following exception:

Internal .NET Framework Data Provider error 1025.

However the exact code minus the cast works:

model.Items = dbQuery.Select(c => new Item {
    Total = c.Items.Count(t => t.Amount.HasValue)
}).ToArray();

Note: dbQuery is just an EF ObjectQuery.

The expected type for Count() is Func<Item, bool>, and it's even verified by ReSharper that they're the same type by the fact that it grays the cast out, suggesting that it's not needed and they're functionally equivalent.

However, the exception proves that they are not, in fact, functionally equivalent.

Also, I did see this related question/answer, but Count() (in this case, anyway) will not accept an Expression<Func<Item bool>>, so that doesn't work for me.

解决方案

You want to "store [the] expression in a variable, for reuse purposes, and pass that into Count()". This is 100% possible if the Expression can be translated to SQL. The problem here is that you're introducing something that can't be translated to SQL. The particular issue in this case is that the cast gets translated to an Expression.Convert. Your types definitely aren't represented in SQL, so this won't be able to execute. Plug both into LINQPad and check out the IL to see the difference.

As the other related question/answer you linked to suggests, you need to pass your predicate as an Expression rather than a Func (how to do the work vs. pointing to a location in code that does the work). The problem, as you noted, is that the Count method doesn't take an Expression, but this is easily resolved by using AsQueryable() which will give you the IQueryable extension methods that take Expression.

This should have no problems executing:

Expression<Func<Item,bool>> predicate = i => i.Amount.HasValue;

model.Items = dbQuery.Select(c => new Item {
    Total = c.Items.AsQueryable().Count(predicate)
}).ToArray();

这篇关于转换LINQ表达式将引发“内部.NET Framework数据提供程序错误1025".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆