Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法强制转换类型为';System.Linq.EnumerableQuery`1[Entities.Test]';输入';System.Data.Objects.ObjectQuery`1[Entities.Test]';_C#_.net_Entity Framework_Linq To Entities_Ef Code First - Fatal编程技术网

C# 无法强制转换类型为';System.Linq.EnumerableQuery`1[Entities.Test]';输入';System.Data.Objects.ObjectQuery`1[Entities.Test]';

C# 无法强制转换类型为';System.Linq.EnumerableQuery`1[Entities.Test]';输入';System.Data.Objects.ObjectQuery`1[Entities.Test]';,c#,.net,entity-framework,linq-to-entities,ef-code-first,C#,.net,Entity Framework,Linq To Entities,Ef Code First,我首先使用Entity Framework 4.2(EntityFramework.dll v4.0.30319)代码,并有一个LINQ查询,可以简化为: IQueryable<Test> testQuery = from test in repository.Tests select test; 我得到一个例外: 无法将“System.Linq.EnumerableQuery1[Entities.Test]”类型的对象强制转换为“System.Data.Objects.Objec

我首先使用Entity Framework 4.2(EntityFramework.dll v4.0.30319)代码,并有一个LINQ查询,可以简化为:

IQueryable<Test> testQuery = from test in repository.Tests select test;
我得到一个例外:

无法将“System.Linq.EnumerableQuery
1[Entities.Test]”类型的对象强制转换为“System.Data.Objects.ObjectQuery
1[Entities.Test]”类型


为什么会发生这种情况?

您正在组合两种不同的API—ObjectContext API和DbContext API。您不能将从
DbSet
创建的查询强制转换为
ObjectQuery
,而只能转换为与
ObjectQuery
无关的
DbQuery

您想做的事情可以通过以下方式轻松实现:

var trace = testQuery.ToString();

以防万一,有人在谷歌上搜索到:查询是区分大小写的,因为它是在单元测试中使用模拟数据(数组,而不是数据库)执行的;-)
var trace = testQuery.ToString();