Linq与自定义基础集合

And*_*asN 7 c# linq

在使用自定义集合对象时,我经常发现linq存在问题.他们经常被打败

基础集合

abstract class BaseCollection<T> : List<T> { ... }
Run Code Online (Sandbox Code Playgroud)

集合定义为

class PruductCollection : BaseCollection<Product> { ... }
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法将linq exssion的结果添加到此集合而不是addrange或concat?

var products = from p in HugeProductCollection
               where p.Vendor = currentVendor
               select p;
PruductCollection objVendorProducts = new PruductCollection();
objVendorProducts.AddRange(products);
Run Code Online (Sandbox Code Playgroud)

如果从linq查询返回的对象属于我的自定义集合类型,那将是很好的.因为您似乎需要两次枚举该集合才能执行此操作.

编辑:阅读答案后,我认为最好的解决方案是实现ToProduct()扩展.想知道c#4.0中的协方差/逆变是否有助于解决这些问题.

tva*_*son 7

问题是LINQ通过扩展方法IEnumerable<T>知道如何构建数组,列表和字典,它不知道如何构建自定义集合.你可以让你的自定义集合有一个构造函数,IEnumerable<T>或者你可以写你.前者允许您直接在构造函数中使用LINQ结果,后者允许您使用扩展来装饰LINQ语句并获取所需的集合.无论哪种方式,您都需要从泛型集合到专用集合进行某种转换 - 无论是在构造函数中还是在扩展中.或者你可以做到两个......

public static class MyExtensions
{
     public static ProductCollection
                      ToProducts( this IEnumerable<Product> collection )
     {
          return new ProductCollection( collection );
     }
}


public class ProductCollection : BaseCollection<Product>
{
     ...

     public ProductCollection( IEnumerable<Product> collection )
              : base( collection )
     {
     }

     ...
 }


var products = (from p in HugeProductCollection
                where p.Vendor = currentVendor
                select p).ToProducts();
Run Code Online (Sandbox Code Playgroud)