实体框架中的类型转换问题

时间:2018-06-15 21:42:54

标签: c# entity-framework linq asp.net-web-api

我的实体框架返回一个IQueryable,包含所有元素但链接在创建列表对象时丢失 该对象(列表)需要将用户数据与列表数据进行比较。如果有人能帮助我,我会感激不尽!这是my first question

MapToBindingModel = title => new TitleBindingModel
{
    Id = title.Id,     
    //data     
    // type conversion when studio's data lost because tolist    
    Studios = title.Studios.Select(i => new StudioBindingModel() { Id = i.Id, StudioName = i.StudioName, FilteredName = i.FilteredName, ImageShiki = i.ImageShiki }).AsQueryable(),               
    Favorite = _applicationUser != null && _applicationUser.Favorite.Titles.Any(i=>i.Id == title.Id),

};

protected ApplicationUserManager UserManager => 
    Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
internal ApplicationUser _applicationUser;

[HttpGet]
[Route]
[EnableQuery]
[AllowAnonymous]
public virtual async Task<IHttpActionResult> GetAll()
{
    _applicationUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());
    return Ok(Service.GetAll().ToList().AsQueryable().Select(MapToBindingModel));
}

   // Repository getall code
    public virtual IQueryable<T> GetAll()
    {
        return DbSet.AsQueryable();
    }

0 个答案:

没有答案