Is it possible to map IQueryable<CatDTO> to IQueryable<CatEf>?

情到浓时终转凉″ 提交于 2020-01-03 12:59:59

问题


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...

I have such classes:

  • 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);

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?


回答1:


I believe the functionality you want is in 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);

When you enumerate it will convert Lambda from CatDto to CatEf and call ProjectTo<CatDto> and return CatDto objects



来源:https://stackoverflow.com/questions/37965593/is-it-possible-to-map-iqueryablecatdto-to-iqueryablecatef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!