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

本文关键字:两个 泛型类型 方法 扩展 | 更新日期: 2023-09-27 18:36:51

现在我的代码是这样的代码:(简化!

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>()

这就是我希望它的样子:

我希望能够写一些东西 lkie 这个:

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方法的两种类型。


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


更新

关于我的代码是如何设置的,似乎有很多疑问,所以这里有一些类(简化)

产品收藏

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 properties
}

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

编辑:根据您最近的更新,我建议如下:

建议:更改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;

这项工作是谁的?

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;
    }
}