C# 将IEnumerable转换为System.Data.Linq.Table
我有一个返回类型为Table的函数 此代码不起作用。我只想将IEnumerable作为Linq.Table返回**C# 将IEnumerable转换为System.Data.Linq.Table,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有一个返回类型为Table的函数 此代码不起作用。我只想将IEnumerable作为Linq.Table返回** public static System.Data.Linq.Table<Products> GetProducts() { MyContext mc = new MyContext(); Table<Products> ret = null; // Or Table<Products&g
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;
}
public static System.Data.Linq.Table GetProducts()
{
MyContext mc=新的MyContext();
表ret=null;
//或表ret=mc.GetTable();
ret.AttachAll(mc.GetTable()
.OrderBy(p=>p.ProductID).Take(1));
返回ret;
}
现在我想将IEnumerable查询转换为System.Data.Linq.Table
(您可能会说我可以更改返回类型,但问题是这是否可行。我是否可以重写某些函数,如GetTable()?)您不能。存在是为了提供实现。其中没有任何实际的项,它只是一个根,由公开,以便提供一个IQueryable
实现,该实现将被解释、转换为SQL,然后发送到SQL server
如果您有一个实现,那么很可能您已经有了正在迭代的具体化结果集(或内存中的集合)。拥有IQueryable
实现没有任何好处,因为没有什么可解释的;你想要的一切都已经在记忆中了
也就是说,在IEnumerable
实现中使用常规扩展方法/查询语法可能比尝试将其放入表中要好得多。存在是为了提供实现。其中没有任何实际的项,它只是一个根,由公开,以便提供一个IQueryable
实现,该实现将被解释、转换为SQL,然后发送到SQL server
如果您有一个实现,那么很可能您已经有了正在迭代的具体化结果集(或内存中的集合)。拥有IQueryable
实现没有任何好处,因为没有什么可解释的;你想要的一切都已经在记忆中了
也就是说,在IEnumerable
实现中使用常规扩展方法/查询语法可能比将其放入表中要好