Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何选择用户在集合中的位置<&燃气轮机;实体框架中的方法_C#_Asp.net Mvc_Entity Framework_Linq_Ef Code First - Fatal编程技术网

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
。是的,extension
IQueryable-Active(这个IDbSet集)
是个好主意。是的,extension
IQueryable-Active(这个IDbSet集)
是个好主意。