從評論,它可能是你真正想要生成將自己的Iitem
枚舉,並採取Product
作爲參數,並返回其所有Iitem
(胡)的Id
的功能相匹配的ItemID
Product
。這是這樣的:
Func<Product, IEnumerable<Iitem>> ItemProduct(IEnumerable<Iitem> items) =>
pro => items.Where(item => pro.ItemID == item.Id);
使用像這樣:
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);
我會離開這裏我原來的猜測,因爲我真不知道你真正想要的。
此方法將返回一個FUNC返回true,如果所有 IDS在items
匹配Product
傳過來的參數ItemID
:
Func<Product, bool> ItemProduct(IEnumerable<Iitem> items) =>
(pro) => items.All(item => pro.ItemID == item.Id);
像這樣:
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);
如果如果至少有一個Ids匹配,但希望該函數返回true,但不一定全部匹配,則會這樣做。唯一的區別是,items.All()
變化items.Any()
:
Func<Product, bool> ItemProductAny(IEnumerable<Iitem> items) =>
(pro) => items.Any(item => pro.ItemID == item.Id);
像這樣:
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);
什麼是'ItemProduct(IEnumerable的項目)'應該做/返回? –
事實上 - 提供一個[mcve]可以真正澄清這一點,在提出它的時候,你可能會回答你自己的問題。 –
@YacoubMassad它應該返回IEnumerable –
reddy