I have a DbContext that is implented IUnitOfWork as below
public interface IUnitOfWork {
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
}
also one interface for all entities & one implentation also for all entities as below
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);
}
and its implementation
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);
}
}
and here is one of classes
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;
}
}
The problem is that if i Use some code like
public IPollService ipolls { get; set; }
public void Test {
int countrecords = ipolls.GetAllCount(x=>true);
}
In Sql Server i get
select * from Entity where predicate not select count(*) from entity where predicate
So i changed implentation and added overrided function in PollService and get result as commented in code. i can not figure out what is happening.
It seems when i use Func<Poll, bool> Directly, i get desired result, but when i use its value from sended argument i get undesired result
other notes: we use asp.net Web Forms, EF Code First version is 5.0 & dev tool is vs2012, also for DI we use StructureMap
Try changing the method signature to use Linq expressions,