Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将IEnumerable转换为System.Data.Linq.Table_C#_Linq_Linq To Sql - Fatal编程技术网

C# 将IEnumerable转换为System.Data.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

我有一个返回类型为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;
    }
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
实现中使用常规扩展方法/查询语法可能比将其放入
表中要好