是否可以映射IQueryable< CatDTO>到IQueryable< CatEf>? [英] Is it possible to map IQueryable<CatDTO> to IQueryable<CatEf>?
问题描述
这是Automapper专业人士的一个问题。我已经尝试了几天的查询映射 - 没有运气。就像Automapper似乎不是要用我想要使用的方式。但也许我错了。所以这里是问题...
This is a question for Automapper professionals. I have tried to do query mapping for several days already - and no luck. Seems like Automapper is not intended to be used the way I want to use it. But maybe I am wrong. So here is the question...
我有这样的课程:
- CatDto(名称,年龄,玩具(ToyDto对象的收藏))
- ToyDto(CatName,ToyName,Cat(parent CatDto object))
- 玩具(来自实体框架,具有与ToyDto类似的属性)
< (来自实体框架,具有与CatDto类似的属性)
- CatDto (Name, Age, Toys (collection of ToyDto objects))
- ToyDto (CatName, ToyName, Cat (parent CatDto object))
- Cat (comes from Entity Framework, has properties similar to those in CatDto)
- Toy (comes from Entity Framework, has properties similar to those in ToyDto)
我想在我的数据访问层写一个通用的读取函数,如下所示:
I want to write a generic read function in my data access layer, something like this:
IEnumerable<CatDto> Read(IQueryable<CatDto> query) {
// here "query" is converted
// to Entity Framework query by means of AutoMapper,
// EF query gets executed,
// I convert EF entities (Cat) back to CatDto - this is not essential
// result is returned
}
我将以不同的方式调用此函数。示例:
I will call this function in different manners. Example:
var q = new ObjectModel.Collection(Of CatDto)).AsQueryable();
q = q.Where(c => c.Toys.Count() > 1);
var someResultVar = Read(q);
到目前为止,任何实现此类行为的尝试都失败了。我不知道Automapper是否是这里的帮手,还是我完全错误的方式?
So far any attempts to implement such behavior have failed. I wonder if Automapper is a helper here or am I going completely wrong way?
推荐答案
我相信你想要的功能在< a href =https://github.com/AutoMapper/AutoMapper/wiki/Expression-Translation-(UseAsDataSource) =nofollow> UseAsDataSource
I believe the functionality you want is in UseAsDataSource
您不能映射IQueryable,但是您不应该使用UseAsDataSource
示例
You can't map IQueryable, but you shouldn't need to with UseAsDataSource Example
IQueryable<CatDto> someResultVar = new ObjectModel.Collection(Of CatDto)).AsQueryable().UseAsDataSource().For(Of OrderLineDTO).Where(c => c.Toys.Count() > 1);
当您枚举它会将Lambda从CatDto转换为CatEf并调用 ProjectTo< CatDto>
并返回CatDto对象
When you enumerate it will convert Lambda from CatDto to CatEf and call ProjectTo<CatDto>
and return CatDto objects
这篇关于是否可以映射IQueryable< CatDTO>到IQueryable< CatEf>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!