user191152
user191152

Reputation:

Does ADO.NET Entity Framework have a construct similar to LINQ to SQL's GetTable<T>?

Does ADO.NET Entity Framework have a construct similar to LINQ to SQL's GetTable?

I'm trying to do something similar in Entity Framework, that I'm very used to doing in LINQ to SQL. Does ADO.NET Entity Framework have the same capability?

LINQ to SQL:

dataContext.GetTable<T>();

It would yield a Queryable collection of type 'T'.

In Entity Framework I would like to do the same:

    //NOTICE: This is a partial extension of the auto-generated Entities-model
    //        created by the designer.
    public partial class Entities : IEntities
    {
      public IQueryable<T> FindAll<T>()
      {
        //pseudo-code
        // return from all entities, the entities that are of type T as Queryable
      }
    }

Since I wasn't able to figure this out, I made, what I consider, a "smelly" work-around:

    //NOTICE: This is a partial extension of the auto-generated Entities-model
    //        created by the designer.
    public partial class Entities : IEntities
    {
      public IQueryable<Users> FindAllUsersQueryable
      {
        get { return Users; }
      }

      public IQueryable<Registrations> FindAllRegistrationsQueryable
      {
        get { return Registrations; }
      }

      //... and so on ...
    }

What I want is something similar to:

    //NOTICE: This is a partial extension of the auto-generated Entities-model
    //        created by the designer.
    public partial class Entities : IEntities
    {
      public IQueryable<T> FindAll<T>()
      {
        return GetEntities<T>();
      }
    }

But so far I haven't been able to find out how to do that.

What is the solution?

Upvotes: 2

Views: 855

Answers (2)

user191152
user191152

Reputation:

    public IQueryable<T> FindAll<T>()
    {
        var baseType = typeof(T);
        return CreateQuery<T>("[" + baseType.Name.ToString() + "]").OfType<T>();
    }

Also seems to do the job for me :-)

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063198

Marking as wiki as I can't claim any credit, but an MSFT answer to this question is here: What is the equivilant of GetTable in L2E.

Upvotes: 1

Related Questions