简体   繁体   English

将IEnumerable转换为System.Data.Linq.Table

[英]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.

相关问题 了解System.Data.Linq.Table <T> - Understanding System.Data.Linq.Table<T> 将System.Data.Linq.Table <T>传递给通用函数 - Passing System.Data.Linq.Table<T> into Generic Function Cast System.Data.Linq.Table <T> 到自定义类 - Cast System.Data.Linq.Table<T> to custom class 我如何编写System.Data.Linq.Table的扩展方法 <T> ? - How would I write an extension method for System.Data.Linq.Table<T>? sqlmetal.exe返回值使用通用类型&#39;System.Data.Linq.Table&#39;需要1个类型参数 - sqlmetal.exe returns Using the generic type 'System.Data.Linq.Table' requires 1 type arguments Linq-无法隐式转换类型&#39;System.Collections.Generic.IEnumerable - Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable 无法将类型 &#39;System.Data.Linq.ISingleResult&lt;&quot;&quot;&gt; 隐式转换为 &#39;System.Collections.Generic.IEnumerable&lt;&quot; &quot;&gt;。 是否存在显式转换? - Cannot implicitly convert type 'System.Data.Linq.ISingleResult<""> to 'System.Collections.Generic.IEnumerable<" ">. An explicit conversion exists? Linq Entity Framework:选择表数据而不将重复数据转换为IEnumerable - Linq Entity Framework: select table data without repeated data into IEnumerable LINQ for System.Collections.IEnumerable - LINQ for System.Collections.IEnumerable 无法将类型System.Linq.IQueryable隐式转换为System Collections.Generic.IEnumerable - Cannot implicitly convert type System.Linq.IQueryable to System Collections.Generic.IEnumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM