The problem is that ToList
returns a List<Product>
, but that's a distinctly different type than Products
. You could provide a constructor of Products
which accepts an IEnumerable<Product>
like this:
public class Products : List<Product> {
public Products(IEnumerable<Product> products) : base(products) {
}
}
Products prods = new Products(filterFunc(param, products));
But if this is all your Products
class does, it's probably a lot simpler to just get rid of it entirely and deal with IEnumerable<Product>
(or List<Product>
) wherever you need to handle a collection of Product
objects:
IEnumerable<Product> products = CreateProducts();
Params param = new Params { Val = "ABC"};
Func<Params, IEnumerable<Product>, IEnumerable<Product>> filterFunc =
(p, r) => r.Where(x => x.Sku == p.Val);
IEnumerable<Product> prods = filterFunc(param, products);
private static IEnumerable<Product> CreateProducts()
{
return new Products[] {
new Product{
Price = 25.00,
Sku = "ABC"
},
new Product{
Price = 134.00,
Sku = "DEF"
},
};
}