Andiih
Andiih

Reputation: 12413

DbContext and Generics

With DataContext I could do something like

public IQueryable<T> All()
{
   return db.CreateObjectSet<T>().AsQueryable();
}

(as part of a generic repository class - the rest of the CRUD actions are handled in a similar manner)

I'm looking to see if the same is possible with DbContext : i.e. can a return a Queryable of T? Entry looks a bit like it might help, but not quite... ideas anyone ?

Upvotes: 1

Views: 1848

Answers (2)

David Anderson
David Anderson

Reputation: 623

If you wish to return a Queryable would be something like:

public virtual IQueryable GetAll()
{
    return dbContext.Set<T>();
}

In fact, I blogged some time ago about how to create a generic repository using EF4.1 take a look:

http://davidandersonlino.net/blog/2011/04/28/generic-repository-pattern-com-entity-framework-4-1/

Hope I answered your question.

Upvotes: 3

David Wick
David Wick

Reputation: 7095

DbSet implements IQueryable

this should work:

public IQueryable All()
{
    return db.Set<T>();
}

Upvotes: 4

Related Questions