views:

161

answers:

2

I'm trying to do something similar in Entity Framework, that I'm very used to doing in Linq-To-Sql. Now my question is if EF has the same capability, that I'm unable to figure out.

Linq-To-Sql:

dataContext.GetTable<T>();

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.

Anyone got a tip?

+2  A: 

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.

Marc Gravell
Yes, thank you. I found that aswell --- So it seems this is something EF still lacks, eventhough it can be added with custom code.
CodeMonkey
You could always investigate the 4.0 beta?
Marc Gravell
I will definately do that no matter. I got my FindAll<T> working with that example. Thanks for pointing me back to the example again :-)
CodeMonkey
It is indeed in EF 4.
Craig Stuntz
Looking forward to it... until then, this approach will satisfy my needs for now.
CodeMonkey
A: 
    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 :-)

CodeMonkey
From what I learned today, one of the major differences from LINQ to SQL to Entity Framework (LINQ to Entities) is the fact that you are coding against the model, not the database. So a real substitute doesn't exist. But the above example surely works.
CodeMonkey
This seems to be the most correct solution (so far that I know of). Marc Gravell's answer is definately not bad, but the solution is less elegant than this. What we have here is simple Entity SQL. I request all objects of type T from EF. And it returns an IQueryable<T>.
CodeMonkey