mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 16:06:21 +00:00
Delegate
This commit is contained in:
parent
69a9bc7a86
commit
9849d5f18d
@ -65,7 +65,34 @@ namespace LanguageFeatures.Controllers
|
|||||||
decimal cartTotal = cart.TotalPrices();
|
decimal cartTotal = cart.TotalPrices();
|
||||||
|
|
||||||
return View("Result", (object)string.Format("Total: {0:c}", cartTotal));
|
return View("Result", (object)string.Format("Total: {0:c}", cartTotal));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ViewResult UseFilterExtensionMethon()
|
||||||
|
{
|
||||||
|
IEnumerable<Product> products = new ShoppingCart
|
||||||
|
{
|
||||||
|
Products = new List<Product>
|
||||||
|
{
|
||||||
|
new Product {Name = "Kayak", Category = "Watersports", Price = 275M },
|
||||||
|
new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
|
||||||
|
new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M },
|
||||||
|
new Product {Name = "Coner flag", Category = "Soccer", Price = 34.95M }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Func<Product, bool> categoryFileter = delegate (Product prod)
|
||||||
|
{
|
||||||
|
return prod.Category == "Soccer";
|
||||||
|
};
|
||||||
|
|
||||||
|
decimal total = 0;
|
||||||
|
|
||||||
|
foreach (Product prod in products.Filter(categoryFileter))
|
||||||
|
{
|
||||||
|
total += prod.Price;
|
||||||
|
}
|
||||||
|
|
||||||
|
return View("Result", (object)String.Format("Total: {0}", total));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,5 +16,27 @@ namespace LanguageFeatures.Models
|
|||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum, string categoryParam)
|
||||||
|
{
|
||||||
|
foreach (Product prod in productEnum)
|
||||||
|
{
|
||||||
|
if (prod.Category == categoryParam)
|
||||||
|
{
|
||||||
|
yield return prod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Product> Filter(this IEnumerable<Product> productEnum, Func<Product, bool> selectorParm)
|
||||||
|
{
|
||||||
|
foreach (Product prod in productEnum)
|
||||||
|
{
|
||||||
|
if (selectorParm(prod))
|
||||||
|
{
|
||||||
|
yield return prod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
|
@Model String
|
||||||
@{
|
@{
|
||||||
Layout = null;
|
Layout = null;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user