Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:15:11+00:00 2026-06-14T13:15:11+00:00

I have a DbContext that is implented IUnitOfWork as below public interface IUnitOfWork {

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:15:12+00:00Added an answer on June 14, 2026 at 1:15 pm

    Try changing the method signature to use Linq expressions,

     int GetAllCount(Expression<Func<T, bool>> predicate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains objects of another class: public class Container {
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
I have created a context class that implements DbContext BeerRecommenderContext and an intializer that
Say I have two DbContext, one containing breeds and one containing dogs. public class
This is the datamodel I have: public class Team { [Key] public int Id
I have an ECMContext class that inherits from DbContext. Within ECMContext there is a
Assume that I have two entities as follows: class Blog { public Blog() {
I have a class that looks like this: public class Analyst { [Column(Internal_ID)] public
I have simple DBcontext class called OdeToFoodDb: public class OdeToFoodDb: DbContext { public DbSet<Restaurant>
I have a DbContext that is empty. Mappings are created dynamically and the DbContext

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.