[英]Convert IEnumerable to System.Data.Linq.Table
I have a function whose return type is Table 我有一个返回类型为Table的函数
This code does not work. 此代码不起作用。 I just want to return the IEnumerable as Linq.Table ** 我只想将IEnumerable返回为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. 现在我想将IEnumerable查询转换为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()? ) (您可能会说我可以更改返回类型,但是问题是这是否可行。我可以重写诸如GetTable()的某些函数吗?)
You can't. 你不能 The Table<TEntity>
class exists in order to provide an IQueryable<T>
interface implementation. 存在Table<TEntity>
类是为了提供IQueryable<T>
接口实现。 There aren't any actual items in there, it's just a root that's exposed by the DataContext
class in order to provide an IQueryable<T>
implementation which is interpreted, translated to SQL and then sent to SQL server. 那里没有任何实际的项目,它只是DataContext
类公开的根,以便提供IQueryable<T>
实现,该实现被解释,翻译为SQL,然后发送给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. 如果您具有IEnumerable<T>
接口实现,则很可能已经具有要迭代的物化结果集(或内存中的集合)。 There's no benefit to having an IQueryable<T>
implementation because there's nothing to interpret; 使用IQueryable<T>
实现没有任何好处,因为没有什么可解释的。 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 a Table<TEntity>
. 就是说,最好不要在IEnumerable<T>
实现中使用常规扩展方法/查询语法,而不是尝试将其放入Table<TEntity>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.