-3

I have a func which looks like below

Func<Product, bool> ItemProduct(Iitem item) => (pro)=> pro.ItemID == item.Id;

I am looking for a func which take multiple items. I am trying like below but that is not correct...Can you please help me how to do this?

Func<Product, bool> ItemProduct(IEnumerable<Iitem> items) => (pro) => pro.ItemID == items.Id;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
reddy
  • 81
  • 2
  • 7

2 Answers2

1

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 Iitems 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);
1

Assuming you need to return a function which for a given product returns true if all items from a given set have the same Id as the product's ItemID, you may use items.All(...) instead of single ID comparison:

Func<Product, bool> ItemProduct1(IEnumerable<Iitem> items) 
    => (pro) => items.All(i => pro.ItemID == i.Id);

If you need true for products which ItemID matches Id of just some of the given items, use items.Any(...) instead:

Func<Product, bool> ItemProduct1(IEnumerable<Iitem> items) 
    => (pro) => items.Any(i => pro.ItemID == i.Id);
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40