尝试返回IQueryable< MyType>时发生转换错误 [英] Casting errors when attempting to return an IQueryable<MyType>
问题描述
我有一个查询,应该返回一个IQueryable<MyType>
.代码如下:
I have a query that ought to return an IQueryable<MyType>
. The code looks like this:
public IQueryable<MyType> GetFooList()
{
var query = (from x in dbContext.TableX
join y in dbContext.TableY on x.our_id equals y.our_id
join z in dbContext.TableZ on y.our_id equals z.our_id
join a in dbContext.TableA on z.other_id equals a.other_id
where !string.IsNullOrEmpty(x.status)
select new
{
(fields....)
})
.AsQueryable();
IQueryable<MyType> result = (IQueryable<MyType>) query;
return result;
}
在调用控制器操作中,我要过滤此列表以获取运行时指定的值;在各种调用操作之间,要过滤的参数将有所不同.例如:
In the calling controller actions, I want to filter this list for values specified at run time; the parameters to filter for will differ between the various calling actions. E.g.:
List<MyType> FooList = Interface.GetFooList()
.Where( specific conditions )
.ToList();
在行设置result
上,引发异常:
On the line setting result
, an exception gets raised:
无效的强制转换异常未由用户代码处理
无法转换类型的对象 'System.Data.Entity.Infrastructure.DbQuery'1 [<> f__AnonymousType9'9 [System.String,System.Nullable`1 [System.DateTime],System.String,System.String,System.String,System.String ,System.Int32,System.Nullable'1 [System.DateTime],System.Nullable'1 [System.DateTime]]]' 键入"System.Linq.IQueryable'1 [MyType]".
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery'1[<>f__AnonymousType9'9[System.String,System.Nullable`1[System.DateTime],System.String,System.String,System.String,System.String,System.Int32,System.Nullable'1[System.DateTime],System.Nullable'1[System.DateTime]]]' to type 'System.Linq.IQueryable'1[MyType]'.
所以我认为这是一个转换问题,并在调用AsQueryable()
之前添加了.Cast<MyType>()
.这会产生另一个错误:
So I figured this was a casting problem, and added .Cast<MyType>()
before the call to AsQueryable()
. This generates a different error:
无法将类型匿名类型"转换为类型"MyType". LINQ至 实体仅支持强制转换EDM基本类型或枚举类型.
Unable to cast the type 'Anonymous type' to type 'MyType'. LINQ to Entities only supports casting EDM primitive or enumeration types.
如果我不进行任何强制转换,则会在调用操作而不是Entity Frameworks访问器中引发这些错误.
And if I don't do any casting, these errors get raised in the calling actions instead of the Entity Frameworks accessor.
我尝试了所有链接的类似问题"中的建议,但无济于事-错误不断出现.我什至尝试包括.Select(obj => new MyType() {fields...} )
来摆脱匿名类型.那也不起作用.
I've tried the suggestions in all the linked "Similar Questions", to no avail -- the errors keep going back and forth. I even tried including .Select(obj => new MyType() {fields...} )
to get away from the anonymous type. That didn't work either.
我感觉自己似乎遗漏了一些明显的东西.
I feel like I'm missing something subtly obvious.
编辑后添加
我更新了代码以选择一种类型:select new MyType() {fields...}
.这工作正常.然后,调用方法在我过滤结果并从查询中列出的行上抛出了 NotSupportedException :
I updated the code to select a type: select new MyType() {fields...}
. This worked properly. Then the calling method threw a NotSupportedException, on the line where I filter the results and make a list from the query:
实体或复杂类型'MyType'不能在 LINQ to Entities查询.
The entity or complex type 'MyType' cannot be constructed in a LINQ to Entities query.
ETA2
我将EF表属性复制到一个新类MyTypeDTO.我用MyTypeDTO替换了对MyType的所有使用.我收到此错误:
I copied the EF table properties to a new class, MyTypeDTO. I replaced all use of MyType with MyTypeDTO. I got this error:
LINQ to Entities不支持指定的类型成员'our_id'.仅支持初始化程序,实体成员和实体导航属性.
The specified type member 'our_id' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
这是DTO中的财产:
public int our_id { get; set; }
因此,我删除了获取/设置,重建并重新运行.不,我遇到了同样的错误.
So I removed the get/set, rebuilt, and reran. Nope, I got the same error.
推荐答案
问题非常难以捉摸.查询的匿名类型中的字段之一在MyType
中不存在.我在创建用于此方法的视图模型时发现了这一点.
The problem was incredibly subtle. One of the fields in the query's anonymous type did not exist in MyType
. I discovered this while I was creating a view model to use in this method.
就其价值而言,视图模型可以正常工作.例如:
For what it's worth, the view model works just fine. Eg:
public IQueryable<MyViewModel> GetFooList(int parameter)
{
var query = (from x in dbContext.TableX
join y in dbContext.TableY on x.our_id equals y.our_id
join z in dbContext.TableZ on y.our_id equals z.our_id
join a in dbContext.TableA on z.other_id equals a.other_id
where x.their_id == parameter
select new MyViewModel()
{
field1 = x.field1,
field2 = y.field2,
field3 = z.field3 //<= this field did not exist in MyType
}
).AsQueryable();
return query;
}
这篇关于尝试返回IQueryable< MyType>时发生转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!