C# 如何选择用户在集合中的位置<&燃气轮机;实体框架中的方法
我有一个C# 如何选择用户在集合中的位置<&燃气轮机;实体框架中的方法,c#,asp.net-mvc,entity-framework,linq,ef-code-first,C#,Asp.net Mvc,Entity Framework,Linq,Ef Code First,我有一个UnitOfWork界面,如下所示: public interface IUnitOfWork { //some other methods IDbSet<TEntity> Set<TEntity>() where TEntity : class; } public class BrandService : IBrandService { private readonly IUnitOfWork _uow; private
UnitOfWork
界面,如下所示:
public interface IUnitOfWork
{
//some other methods
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
}
public class BrandService : IBrandService
{
private readonly IUnitOfWork _uow;
private readonly IDbSet<Brand> _brands;
public BrandService(IUnitOfWork uow)
{
_uow = uow;
_brands = _uow.Set<Brand>();
}
}
_brands = _uow.Set<Brand>().Where(row=>row.IsActive == true);
public static IQueryable<TEntity> ACtive<TEntity>(this IDbSet<TEntity> set) where TEntity : BaseEntity
{
return set.Where(row=>row.IsActive==true);
}
public async Task<Brand> GetAsync(Guid brandId)
{
return await _brands.Active().Where(row => row.Id == brandId).FirstOrDefaultAsync();
}
public async Task<IEnumerable<Brand>> GetAllAsync()
{
return await _brands.Active().ToListAsync();
}
我希望在设置后使用服务层中的where方法,如下所示:
public interface IUnitOfWork
{
//some other methods
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
}
public class BrandService : IBrandService
{
private readonly IUnitOfWork _uow;
private readonly IDbSet<Brand> _brands;
public BrandService(IUnitOfWork uow)
{
_uow = uow;
_brands = _uow.Set<Brand>();
}
}
_brands = _uow.Set<Brand>().Where(row=>row.IsActive == true);
public static IQueryable<TEntity> ACtive<TEntity>(this IDbSet<TEntity> set) where TEntity : BaseEntity
{
return set.Where(row=>row.IsActive==true);
}
public async Task<Brand> GetAsync(Guid brandId)
{
return await _brands.Active().Where(row => row.Id == brandId).FirstOrDefaultAsync();
}
public async Task<IEnumerable<Brand>> GetAllAsync()
{
return await _brands.Active().ToListAsync();
}
\u brands=\u uow.Set()。其中(row=>row.IsActive==true);
但它返回一个错误:
无法将类型“System.Linq.IQueryable”隐式转换为“System.Data.Entity.IDbSet”
我怎么做
我在谷歌上搜索过,但找不到类似的问题
我使用了以下代码:
_uow = uow;
_brands = _uow.Set<Brand>();
var data = _uow.Set<Brand>().Where(e => e.IsDeleted == false);
_brands = (IDbSet<Brand>)data.ToList();
\u uow=uow;
_品牌=_uow.Set();
var data=_uow.Set(),其中(e=>e.IsDeleted==false);
_brands=(IDbSet)data.ToList();
但是它返回
buildPlan.Cs
notfound您不能将IQueryable
分配给IDbSet
。将您的声明更改为
private IQueryable<Brand> _brands;
私人IQueryable\u品牌;
您不能将IQueryable
分配给IDbSet
。将您的声明更改为
private IQueryable<Brand> _brands;
私人IQueryable\u品牌;
romanoza tahnks谢谢你的回答。
如果我在Service Constructor
中为IsActive
等于True
的所有行筛选数据,我无法向用户显示IsActive=false
的所有行,为此,我需要以下方法:
public interface IUnitOfWork
{
//some other methods
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
}
public class BrandService : IBrandService
{
private readonly IUnitOfWork _uow;
private readonly IDbSet<Brand> _brands;
public BrandService(IUnitOfWork uow)
{
_uow = uow;
_brands = _uow.Set<Brand>();
}
}
_brands = _uow.Set<Brand>().Where(row=>row.IsActive == true);
public static IQueryable<TEntity> ACtive<TEntity>(this IDbSet<TEntity> set) where TEntity : BaseEntity
{
return set.Where(row=>row.IsActive==true);
}
public async Task<Brand> GetAsync(Guid brandId)
{
return await _brands.Active().Where(row => row.Id == brandId).FirstOrDefaultAsync();
}
public async Task<IEnumerable<Brand>> GetAllAsync()
{
return await _brands.Active().ToListAsync();
}
我觉得这样更好。
你的意见是什么?罗曼诺莎·塔恩克等待你的回答。 如果我在
Service Constructor
中为IsActive
等于True
的所有行筛选数据,我无法向用户显示IsActive=false
的所有行,为此,我需要以下方法:
public interface IUnitOfWork
{
//some other methods
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
}
public class BrandService : IBrandService
{
private readonly IUnitOfWork _uow;
private readonly IDbSet<Brand> _brands;
public BrandService(IUnitOfWork uow)
{
_uow = uow;
_brands = _uow.Set<Brand>();
}
}
_brands = _uow.Set<Brand>().Where(row=>row.IsActive == true);
public static IQueryable<TEntity> ACtive<TEntity>(this IDbSet<TEntity> set) where TEntity : BaseEntity
{
return set.Where(row=>row.IsActive==true);
}
public async Task<Brand> GetAsync(Guid brandId)
{
return await _brands.Active().Where(row => row.Id == brandId).FirstOrDefaultAsync();
}
public async Task<IEnumerable<Brand>> GetAllAsync()
{
return await _brands.Active().ToListAsync();
}
我觉得这样更好。
您的意见是什么?谢谢,但我想使用Idbset,我不想使用
IEnumerable
或IQueryable
@UthmanRahimi不幸的是,无法将查询结果分配给dbset
。这样做的意义是什么:\u brands=\u uow.Set().Where(row=>row.IsActive==true)_品牌。添加(新品牌(){…})
?您不能将实体添加到查询中。我添加了一个扩展方法,并像使用\u brands.Active().OrderBy(row=>row.code)
一样使用它,您觉得怎么样?@UthmanRahimi是的,这是个好主意。但是Active()
的结果仍然是IQueryable
,而不是IDbSet
。谢谢,但我想使用IDbSet,我不想使用IEnumerable
或IQueryable
@UthmanRahimi。不幸的是,无法将查询结果分配给dbset
。这样做的意义是什么:\u brands=\u uow.Set().Where(row=>row.IsActive==true)_品牌。添加(新品牌(){…})
?您不能将实体添加到查询中。我添加了一个扩展方法,并像使用\u brands.Active().OrderBy(row=>row.code)
一样使用它,您觉得怎么样?@UthmanRahimi是的,这是个好主意。但是Active()
的结果仍然是IQueryable
,而不是IDbSet
。是的,extensionIQueryable-Active(这个IDbSet集)
是个好主意。是的,extensionIQueryable-Active(这个IDbSet集)
是个好主意。