From comments, it may be that you actually want to generate a function that will own an enumeration of Iitem
and take a Product
as a parameter, and will return all of its Iitem
s whose Id
matches the ItemID
of the Product
. That's this:
Func<Product, IEnumerable<Iitem>> ItemProduct(IEnumerable<Iitem> items) =>
pro => items.Where(item => pro.ItemID == item.Id);
Used like so:
var someItems = new [] { new Iitem { Id = 1 }, new Iitem { Id = 2 } };
var f = ItemProduct(someItems);
var prod = new Product { ItemID = 1; }
// Results will be an enumeration that will enumerate a single Iitem, the
// one whose Id is 1.
var results = f(prod);
I'll leave my original guess here, because I'm still not sure what you really want.
This method will return a func that returns true if all Ids in items
match the ItemID
of the Product
passed in as a parameter:
Func<Product, bool> ItemProduct(IEnumerable<Iitem> items) =>
(pro) => items.All(item => pro.ItemID == item.Id);
Like so:
var product = new Product() { ItemID = 1 };
var itemColl1 = new Iitem[] { new Iitem { Id = 1 }, new Iitem { Id = 2 } };
var itemColl2 = new Iitem[] { new Iitem { Id = 1 }, new Iitem { Id = 1 } };
var f1 = ItemProduct(itemColl1);
var f2 = ItemProduct(itemColl2);
bool thisWillBeFalse = f1(product);
bool thisWillBeTrue = f2(product);
If you want the function to return true if at least one of the Ids matches, but not necessarily all of them, this would do that. The only difference is that items.All()
changes to items.Any()
:
Func<Product, bool> ItemProductAny(IEnumerable<Iitem> items) =>
(pro) => items.Any(item => pro.ItemID == item.Id);
Like so:
var f3 = ItemProductAny(itemColl1);
var f4 = ItemProductAny(itemColl2);
bool thisWillAlsoBeTrue = f3(product);
bool thisWillAlsoBeTrueAgain = f4(product);
var itemColl3 = new Iitem[] { new Iitem { Id = 2 }, new Iitem { Id = 3 } };
var f5 = ItemProductAny(itemColl3);
bool butThisWillBeFalse = f5(product);