0

我有一个实现 IUnitOfWork 的 DbContext,如下所示

public interface IUnitOfWork {
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}

还为所有实体提供了一个接口,也为所有实体提供了一个实现,如下所示

public interface IRayanService<T> : where T : class {
    void Add(T entity);
    void Delete(T entity);
    T Find(Func<T, bool> predicate);
    IList<TResult> GetAll<TResult>(Func<T, bool> predicate, Func<T, TResult> selector);
    IList<T> GetAll(Func<T, bool> predicate);
    int GetAllCount(Func<T, bool> predicate);
}

及其实施

public class RayanService<TEntity> : IRayanService<TEntity>
    where TEntity : class {

    protected IUnitOfWork _uow;
    protected IDbSet<TEntity> _tEntities;

    public RayanService(IUnitOfWork uow) {
        _uow = uow;
        _tEntities = _uow.Set<TEntity>();
    }

    public virtual void Add(TEntity entity) {
        _tEntities.Add(entity);
    }

    public void Delete(TEntity entity) {
        _tEntities.Remove(entity);
    }

    public TEntity Find(Func<TEntity, bool> predicate) {
        return _tEntities.Where(predicate).FirstOrDefault();
    }

    public IList<TEntity> GetAll(Func<TEntity, bool> predicate) {
        return _tEntities.Where(predicate).ToList();
    }

    public IList<TResult> GetAll<TResult>(Func<TEntity, bool> predicate, Func<TEntity, TResult> selector) {
        return _tEntities.Where(predicate).Select(selector).ToList();
    }
            //when using this we get select * from entity!
    public virtual int GetAllCount(Func<TEntity, bool> predicate) {
        return _tEntities.Count(predicate);
    }
}

这是一类

public interface IPollService : IRayanService<Poll> {

}

public class PollService : RayanService<Poll>, IPollService {
    public PollService(IUnitOfWork uow)
        : base(uow) {
    }

    public override int GetAllCount(Func<Poll, bool> predicate) {
                    // genrates select count(*) 
        int result = _tEntities.Where(x => true).Count();

                    // genrates select *
        result = _tEntities.Where(predicate).Count();
                    // genrates select count(*) 
        var ttt = _tEntities.Select(x => x.ID);
        result = ttt.Where(x => true).Count();
                    // genrates select *
        var yyy = _tEntities.Where(predicate);
        var zzz = yyy.Select(x=>x.ID);
        result = yyy.Select(x => x.ID).Count();
        result = zzz.Count();

                    // genrates select count(*)             
        result = _tEntities.Count();
                    // genrates select * 
        result = _tEntities.Count(predicate);
                    // genrates select count(*) 
        result = _tEntities.Count(x=>true);

        return result;          
    }
}

问题是,如果我使用一些代码,例如

    public IPollService ipolls { get; set; }

    public void Test {
              int countrecords = ipolls.GetAllCount(x=>true);
    }

在 Sql Server 中我 select * from Entity where predicate没有select count(*) from entity where predicate

所以我改变了实现并添加了覆盖函数PollService并得到了代码中注释的结果。我不知道发生了什么。

似乎当我Func<Poll, bool>直接使用时,我得到了想要的结果,但是当我从发送的参数中使用它的值时,我得到了不想要的结果

其他注意事项:我们使用asp.net Web FormsEF Code First版本是 5.0 & 开发工具是vs2012,也用于我们使用的 DIStructureMap

4

1 回答 1

3

尝试更改方法签名以使用 Linq 表达式,

 int GetAllCount(Expression<Func<T, bool>> predicate);
于 2012-11-19T07:41:16.903 回答