e.g. 5.18 Using a Delegate in an Extension Method
e.g. 5.19 Using the Filtering Extension Method with a Func
This commit is contained in:
parent
61cf1cbfaf
commit
be286cffbd
@ -20,8 +20,15 @@ namespace automatically_implemented_properties
|
|||||||
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
|
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Func<Product, bool> categoryFilter = delegate(Product prod)
|
||||||
|
{
|
||||||
|
return prod.Category == "Soccer";
|
||||||
|
};
|
||||||
|
|
||||||
foreach (Product prod in products.FilterByCategory("Soccer"))
|
IEnumerable<Product> filteredProducts = products.Filter(categoryFilter);
|
||||||
|
|
||||||
|
foreach (Product prod in filteredProducts)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
|
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
|
||||||
}
|
}
|
||||||
@ -55,5 +62,17 @@ namespace automatically_implemented_properties
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum,
|
||||||
|
Func<Product, bool> selectorParam)
|
||||||
|
{
|
||||||
|
foreach (Product prod in productEnum)
|
||||||
|
{
|
||||||
|
if (selectorParam(prod))
|
||||||
|
{
|
||||||
|
yield return prod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user