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

  • SEARCH
  • Home
  • 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 8316989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:29:05+00:00 2026-06-08T21:29:05+00:00

I have read a number of posts regarding the implementation of a generic repository.

  • 0

I have read a number of posts regarding the implementation of a generic repository. I have also read a number of posts that explain the difference between exposing IEnumerable vs. IQueryable from my repository.

I would like the flexibility of having my data filtered in the database (rather than in memory by the client) but want to avoid having to define a separate repository interface for all of my entities (and concrete classes that implement those interfaces).

So far my repository looks like this:

public interface IRepository<T>
{
    IEnumerable<T> GetAll();
    IEnumerable<T> Find(Expression<Func<T, bool>> where);

    void Add(T entity);
    void Attach(T entity);
    void Delete(T entity);
}

and an example of a concrete implementation is:

public class Repository<T> : IRepository<T> where T : class
{
    private DbContext _context;
    private DbSet<T> _entitySet;

    public Repository(DbContext context)
    {
        _context = context;
        _entitySet = _context.Set<T>();
    }

    public IEnumerable<T> GetAll()
    {
        return _entitySet;
    }

    public IEnumerable<T> Find(Expression<Func<T, bool>> where)
    {
        return _entitySet.Where(where);
    }

    public void Add(T entity)
    {
        _entitySet.Add(entity);
    }

    public void Attach(T entity)
    {
        _entitySet.Attach(entity);
    }

    public void Delete(T entity)
    {
        _entitySet.Remove(entity);
    }
}

In this case my repository uses DbContext so what I would like to know is how this works with the generic interface:

  1. IQueryable<T> derives from IEnumerable<T>. In My find method I am returning an IQueryable<T> object but the client only sees this as an IEnumerable<T>. Does this mean that if I carry out any subsequent queries on the IEnumerable<T> object it will actually perform the operations on the database and only return the results (because the object is a IQueryable in this case)? Or,
  2. Only the Where clause that is passed into the Find method is executed on the database and any subsequent queries that are performed on the IEnumerable<T> object are performed on the client. Or,
  3. Neither of these happen and I have totally misunderstood how IEnumarable<T>, IQueryable<T> and Linq works.

Update:

I am actually fairly surprised at the answers I have received in the comments. My original repository returned IQueryable and subsequent research led me to believe this was a bad thing to do (e.g. if my viewModel accepts a repository in its constructor it can call any query it wants which makes it is more difficult to test).

All solutions I have seen for this so far involve create entity specific repositories so that IQueryable is not exposed (The only difference I guess is that I am doing this in a generic way).

  • 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-08T21:29:06+00:00Added an answer on June 8, 2026 at 9:29 pm

    Because you are returning IEnumerable<T> all subsequent calls will be done in memory (locally).

    One thing to keep in mind is that LINQ operates with a set of extension methods. There are extension methods for IQueryable<T> which support all of the necessary wiring to perform queries in locations other than locally, and extension methods for IEnumerable<T> that just work locally.

    Note that which of these is selected is based on the compile time type, not the run time type. So an IQueryable that is cast to an IEnumerable will be treated as an IEnumerable. This is different than the way class normally work (thanks to polymorphism) however it allows the call site control over how to perform these operations.

    An example of where this is useful is when you need to get all of the records in the table and then count them. There is no need to perform the count in SQL if you are going to get all the results anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there have been a number of posts regarding the height:100% declaration
I have read that you should keep the number of connections in your database
I have read many posts regarding deserialization of nullable fields but have not run
I have read most of the posts on here regarding floating point, and I
I have read all the posts about DTMF and iPhone. I know that the
Okay, I've read a number of other posts on StackOverflow regarding multi-threading, but I
I've read over a number of posts regarding DB table design for a common
I am very much confused between instance variables and property. I have read number
I have read a number of posts about this but none with any solid
I have read a number of posts about this and I'm still confused. I'm

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.