Hi,
I'm looking for an equivalent of the DataContext.GetTable<TEntity>
in Entity Framework.
I've found the ObjectContext.CreateQuery<T>
method but it is different from DataContext.GetTable<TEntity>
since it needs a querystring to work.
Is there a way to get an IQueryable object for a table using the entity type without specifying the querystring?
*EDIT: Added code snippet*
This is a snippet of a Repository class I've implemented that works with linq2sql. I can't use ObjectContext.[TableName]
because it wouldn't be generic anymore.
public class BaseRepository<TClass> : IDisposable
where TClass : class
{
protected BaseRepository(DataContext database)
{
_database = database;
}
...
public IQueryable<TClass> GetAllEntities()
{
IQueryable<TClass> entities = _database.GetTable<TClass>();
return entities;
}
public IQueryable<TClass> GetEntities(Expression<Func<TClass, bool>> condition)
{
IQueryable<TClass> table = _database.GetTable<TClass>();
return table.Where(condition);
}
*EDIT: Added my solution (so far..)*
This is what I'm using:
public IQueryable<TClass> GetEntities(Expression<Func<TClass, bool>> condition)
{
IQueryable<TClass> table = _database.CreateQuery<TClass>(typeof(TClass).Name);
return table.Where(condition);
}
This works as long as the class name is the same of the table name. This will became a problem for me when I'll start using different objects for the same table.
I hope I've been clear, thanks in advance,
Marco :)