How to cast IQueryable<T> to DbSet<T>?

会有一股神秘感。 提交于 2021-01-27 13:02:41

问题


public DbSet<Item> Items 
{ 
    get 
    { 
        return dbContext.Item.Where(x => x.Id == id).Select(x=>x)
    }
} 

The above code causes a compilation error:

Cannot implicitly convert type 'System.Linq.IQueryable to ... DbSet.
An explicit conversion exists (are you missing a cast?)

After adding the explicit cast:

public DbSet<Item> Items 
{
     get 
     { 
         return (DbSet<Item>)(dbContext.Item.Where(x => x.Id == id).Select(x => x))
     }
} 

a runtime error happens:

Additional information: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'1[Item]' to type `'Microsoft.EntityFrameworkCore.DbSet'1[Item]'

Any ideas?

来源:https://stackoverflow.com/questions/43436980/how-to-cast-iqueryablet-to-dbsett

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