[英]An unhandled exception of type 'System.InvalidCastException' occurred in UretimPlan.exe
[英]An unhandled exception of type 'System.InvalidCastException'
我的C#VS2015应用程序抛出此异常:
未处理System.InvalidCastException
Message =无法转换类型为'System.Data.Entity.Infrastructure.DbQuery`1 [ADONET4LINQtoENTITIES.Customer]'的对象来键入'System.Data.Objects.ObjectQuery'。
来源= ADONET4LINQtoENTITIES
错误发生在定义查询变量的行中:
var customers = from c in de.Customers
where c.Orders.Any(o => o.OrderAmount > 150)
select c;
string query = ((ObjectQuery)customers).ToTraceString();
我的假设是ObjectQuery
会引发异常。
我尝试使用string query = ((DbQuery)customers).ToString()
,它在VS2013中有效,但这导致了相同的异常。
为什么VS2015用这些查询方法抛出异常?
因为Customers
不是ObjectQuery
,所以您需要的是ObjectQuery<Customer>
因此ObjectQuery<Customer>
删除投影( Select
)。
改成
var CustomersQuery= from c in de.Customers
where c.Orders.Any(o => o.OrderAmount > 150);
string query = CustomersQuery.ToTraceString();
我通过将其命名为CustomersQuery
来消除命名歧义。
有关更多信息,请参见ObjectQuery.ToTraceString方法(System.Data.Objects) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.