从评论,它可能是你真正想要生成将自己的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