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 564097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:43:49+00:00 2026-05-13T12:43:49+00:00

I’m looking for an equivalent of the DataContext.GetTable<TEntity> in Entity Framework. I’ve found the

  • 0

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 🙂

  • 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-05-13T12:43:50+00:00Added an answer on May 13, 2026 at 12:43 pm

    Actually, the EF designer itself uses CreateQuery with hard-coded strings for the static references. If you dig into the designer file you’ll see something like this:

    public global::System.Data.Objects.ObjectQuery<Customers> Customers
    {
        get
        {
            if ((this._Customers == null))
            {
                this._Customers = base.CreateQuery<Customers>("[Customers]");
            }
            return this._Customers;
        }
    }
    
    private global::System.Data.Objects.ObjectQuery<Customers> _Customers;
    

    Technically there’s no perfect solution because you can use the same entity type for different entity sets. But you can give it the old college try:

    public IQueryable<TEntity> GetEntities<TEntity>()
    {
        Type t = typeof(TEntity);
        var edmAttr = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(t,
            typeof(EdmEntityTypeAttribute), false);
        if (edmAttr == null)  // Fall back to the naive way
        {
            return context.CreateQuery<TEntity>(t.Name);
        }
        var ec = context.MetadataWorkspace.GetEntityContainer(
            context.DefaultContainerName, DataSpace.CSpace);
        var entityType = context.MetadataWorkspace.GetType(edmAttr.Name,
            edmAttr.NamespaceName, DataSpace.CSpace);
        var es = ec.BaseEntitySets.First(es => es.ElementType == entityType);
        return context.CreateQuery<TEntity>(es.Name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I need to clean up various Word 'smart' characters in user input, including but

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.