I have a function whose return type is Table
This code does not work. I just want to return the IEnumerable as Linq.Table **
public static System.Data.Linq.Table<Products> GetProducts()
{
MyContext mc = new MyContext();
Table<Products> ret = null;
// Or Table<Products> ret = mc.GetTable<Products>();
ret.AttachAll<Products>(mc.GetTable<Products>()
.OrderBy(p => p.ProductID).Take(1));
return ret;
}
Now I want to cast IEnumerable query to System.Data.Linq.Table.
(You may say that I can change the return type but the question is whether this is possible. Can I override some function like GetTable()? )
You can’t. The
Table<TEntity>class exists in order to provide anIQueryable<T>interface implementation. There aren’t any actual items in there, it’s just a root that’s exposed by theDataContextclass in order to provide anIQueryable<T>implementation which is interpreted, translated to SQL and then sent to SQL server.If you have an
IEnumerable<T>interface implementation, chances are you already have the materialized result set (or an in-memory collection) that you are iterating over. There’s no benefit to having anIQueryable<T>implementation because there’s nothing to interpret; everything you want is already in memory.That said, it’s probably better to just use the regular extension methods/query syntax off your
IEnumerable<T>implementation than trying to put it in aTable<TEntity>.