ADO.NET实体框架是否具有类似于LINQ to SQL的GetTable <t>?</t>的结构

时间:2009-10-16 11:32:25

标签: c# linq-to-sql entity-framework ado.net

ADO.NET Entity Framework是否有类似于LINQ to SQL的GetTable的构造?

我正在尝试在Entity Framework中做类似的事情,我很习惯在LINQ to SQL中做。 ADO.NET Entity Framework具有相同的功能吗?

LINQ to SQL:

dataContext.GetTable<T>();

它将产生一个类型为'T'的Queryable集合。

在实体框架中我想做同样的事情:

    //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
      }
    }

由于我无法解决这个问题,我提出了一个“臭臭”的解决办法:

    //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 ...
    }

我想要的是类似于:

    //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>();
      }
    }

但到目前为止,我还没有找到如何做到这一点。

解决方案是什么?

2 个答案:

答案 0 :(得分:2)

标记为维基,因为我无法声明任何功劳,但MSFT对此问题的回答是:What is the equivilant of GetTable in L2E

答案 1 :(得分:0)

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

似乎也为我做的工作: - )