How to resolve - unable to cast Generic.List to Linq.IQueryable using Automapper?

拟墨画扇 提交于 2019-12-23 12:43:05

问题


I'm getting the following error in using a DTO and EF model:

Unable to cast object of type 'System.Collections.Generic.List1[Project.Core.UI.Models.ContentTypes]' to type 'System.Linq.IQueryable1[Project.Core.UI.Models.ContentTypes]

Bootstrapped: Mapper.CreateMap<ContentType, Project.Core.UI.Models.ContentTypes>();

In an OData controller method public IQueryable<ContentTypes> Get() {...}, using:

var result = Mapper.Map<IQueryable<ContentType>, IQueryable<ContentTypes>>(_repository.Query().Get()
    .Where(u => u.UserId == userId)
    .OrderBy(o => o.Description));

I have also tried the following but I suspect that this is exactly what the above is as well:

var result =_repository.Query().Get()
    .Where(u => u.UserId == userId)
    .OrderBy(o => o.Description);
var dto = Mapper.Map<IQueryable<ContentType>, IQueryable<ContentTypes>>(result);
return dto;

How can I create the appropriate mapping for this?


回答1:


You need to be aware of the fact that by creating your mapping in such a way:

Mapper.CreateMap<ContentType, Project.Core.UI.Models.ContentTypes>();

Only these basic generic collection types are supported implicitly:

IEnumerable
IEnumerable<T>
ICollection
ICollection<T>
IList
IList<T>
List<T>
Arrays

But not IQueryable<T>. That's why you get the exception.

Therefore you should try doing sth like this:

var dto = Mapper.Map
         <IEnumerable<ContentType>, 
          IEnumerable<ContentTypes>>(result.AsEnumerable()); 
// or e.g. List/List/ToList()



回答2:


In addition to what bejger said you can also use the QueryableExtensions namespace in Automapper to map IQueryable objects, but it puts some limitations on what setup you can do between two object when you do your CreateMap<TSource, TDest>

instead of using it like

IQueryable<ContentTypes> dto = Mapper.Map<IQueryable<ContentType>, IQueryable<ContentTypes>>(result);

you instead use it like

IQueryable<ContentTypes> dto = result.Project().To<ContentTypes>();



回答3:


You can use the 'ProjectTo' method instead.

var result = iQueryableResultSet.ProjectTo<MyObjectName>(_mapper.ConfigurationProvider);

Optionally you pass the ConfigurationProvider from any injected Mapper instance, as necessary.



来源:https://stackoverflow.com/questions/21648040/how-to-resolve-unable-to-cast-generic-list-to-linq-iqueryable-using-automapper

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