Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# 平均EF通用存储库_C#_Entity Framework_Generics - Fatal编程技术网

C# 平均EF通用存储库

C# 平均EF通用存储库,c#,entity-framework,generics,C#,Entity Framework,Generics,我想将计算平均值的方法添加到我的实体框架通用存储库中。我能做到这一点吗?我应该用什么方法?以下是我正在思考的一些事情(这只是一个坏例子作为起点) 公共双精度GetAverage(字符串属性,表达式筛选器=null) { 返回BuildQuery(filter).Select(property).Average(); } 或者这样做是个坏主意,我应该为特定的实体创建特定的接口,在这里实现是明确的 我现在的回购协议是 public interface IGenericRepository<T

我想将计算平均值的方法添加到我的实体框架通用存储库中。我能做到这一点吗?我应该用什么方法?以下是我正在思考的一些事情(这只是一个坏例子作为起点)

公共双精度GetAverage(字符串属性,表达式筛选器=null) { 返回BuildQuery(filter).Select(property).Average(); } 或者这样做是个坏主意,我应该为特定的实体创建特定的接口,在这里实现是明确的

我现在的回购协议是

public interface IGenericRepository<TEntity> where TEntity : class
{
    void Delete(object id);
    void Delete(TEntity entityToDelete);

    TEntity GetFirst(Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");

    IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");

    PagedList<TEntity> GetPaged(int pageSize, int pageNumber, Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");

    TEntity GetByID(object id);
    void Insert(TEntity entity);
    void Update(TEntity entityToUpdate);
    }
公共接口IGenericRepository,其中tenty:class
{
作废删除(对象id);
无效删除(TEntity entityToDelete);
TEntity GetFirst(表达式过滤器=null,
Func orderBy=null,字符串includeProperties=“”);
IEnumerable Get(表达式过滤器=null,
Func orderBy=null,字符串includeProperties=“”);
PagedList GetPaged(int pageSize,int pageNumber,表达式筛选器=null,
Func orderBy=null,字符串includeProperties=“”);
TEntity GetByID(对象id);
无效插入(TEntity实体);
无效更新(TEntity entityToUpdate);
}
BuildQuery是:

private IQueryable<TEntity> BuildQuery(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
{
    IQueryable<TEntity> query = _dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    foreach (var includeProperty in includeProperties.Split
        (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
        query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
        return orderBy(query);
    }
    else
    {
        return query;
    }
}
private IQueryable BuildQuery(
表达式筛选器=空,
Func orderBy=null,
字符串includeProperties=“”)
{
IQueryable查询=_dbSet;
if(过滤器!=null)
{
query=query.Where(过滤器);
}
foreach(includeProperty.Split中的var includeProperty
(新字符[]{',},StringSplitOptions.RemoveEmptyEntries)
{
query=query.Include(includeProperty);
}
if(orderBy!=null)
{
返回订单人(查询);
}
其他的
{
返回查询;
}
}

我在使用实体框架时遇到了架构设计问题,我决定使用存储库模式并将所有内容抽象到冗余层之后,现在我遇到了更多的架构设计问题,比如说
[“abc”,“cba”,3,“ddd”]
?(请记住,
TEntity
可能属于
object
类型,我知道使用泛型可能是个坏主意。我仍然可以为特定实体使用特定接口(非泛型)。只需删除所有这些内容并直接使用实体框架,或者至少从“存储库”中公开
IQueryable
),并让调用方对其执行任何需要的操作。
private IQueryable<TEntity> BuildQuery(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "")
{
    IQueryable<TEntity> query = _dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    foreach (var includeProperty in includeProperties.Split
        (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
        query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
        return orderBy(query);
    }
    else
    {
        return query;
    }
}