扩展方法中的两种泛型类型

时间:2013-10-25 16:57:20

标签: c# .net linq generics extension-methods

现在我的代码代码看起来像这样:(简体!)

public static IQueryable<T> ListedProducts<T,V>(this IQueryable<T> collection) 
     where T : ProductCollection<V> 
     where V: Product
{
     return collection.Where(x => x.Listed == true);
}

要使用它,我必须像这样定义两种类型:

SomeCollection.ListedProducts<BikeCollection,BikeProduct>()

我希望如此:

我希望能够写出一些东西:

public static IQueryable<T> ListedProducts<T<V>>(this IQueryable<T<V>> collection)
     where T : ProductCollection<V>
{
     return collection.Where(x => x.Listed == true);
}

我只需要写:

SomeCollection.ListedProducts()

我认为这是可能的,因为“SomeCollection”包含通用的ListedProducts方法的两种类型。


希望我的问题足够清楚并且有一个解决方案:)


更新

我的代码设置方式似乎很多,所以这里有一些类(简化)

ProductCollection

public class ProductCollection<T> where T : Product
{
    public int Id { get; set; }
    public string CollectionName { get; set; }
    public virtual ICollection<T> Products { get; set; }
    public bool Listed { get; set; }
}

ProductCollection

public class BikeCollection : ProductCollection<BikeProduct>
{
   //Bike specific properties
}

2 个答案:

答案 0 :(得分:4)

修改:根据您最近的更新,我建议如下:

建议:更改ProductCollection<T>以便它实现IEnumerable<T>

public class ProductCollection<T> : IEnumerable<T>
  where T : Product
{
  public int Id { get; set; }
  public string CollectionName { get; set; }
  public virtual ICollection<T> Products { get; set; }
  public bool Listed { get; set; }

  // This is all it takes to implement IEnumerable<T>
  public IEnumerator<T> GetEnumerator()   { return this.Products.GetEnumerator(); }
  IEnumerator IEnumerable.GetEnumerator() { return this.Products.GetEnumerator(); }
}

然后,您可以通过以下方式更改您的扩展程序:

public static IEnumerable<T> ListedProducts<T>(this IEnumerable<T> collection) 
  where T : Product
{
  return collection.Where(x => x.Listed == true);
}

这使您可以执行以下操作:

// WHERE  BikeCollection : ProductCollection<BikeProduct>
// AND    BikeProduct : Product
var someCollection = new BikeCollection();

// What you want
var listedBikes1 = someCollection.ListedProducts();

// Another way you can do it, if ProductCollection<T> : IEnumerable<T>
var listedBikes2 =
  from product in someCollection
  where product.Listed
  select product;

答案 1 :(得分:0)

这会有用吗?

public static class StaticFunctions
{
    public static IQueryable<ProductCollection<T>> ListedProducts<T>(this IQueryable<ProductCollection<T>> collection)
    where T : Product
    {
        return collection.Where(x => x.Listed == true);
    }
}

public class ProductCollection<T>
    where T:Product
{
    public int Id { get; set; }
    public string CollectionName { get; set; }
    public virtual ICollection<T> Products { get; set; }
    public bool Listed { get; set; }
}

public class BikeCollection : ProductCollection<BikeProduct>
{
    //Bike specific collection
}

public class BikeProduct:Product
{
    //Can be anything
}

public class Product
{
    //Can be anything
}

public partial class Form1 : Form
{


    public Form1()
    {
         InitializeComponent();
         IQueryable<ProductCollection<BikeProduct>> titi = new EnumerableQuery<ProductCollection<BikeProduct>>(new List<ProductCollection<BikeProduct>>());

        titi.ListedProducts();

        var toto = 1;
    }
}