1
0
mirror of https://github.com/rudollee/LearningMVC.git synced 2025-06-07 16:06:21 +00:00
This commit is contained in:
wook 2017-04-03 02:31:15 +09:00
parent 69a9bc7a86
commit 9849d5f18d
3 changed files with 50 additions and 1 deletions

View File

@ -65,7 +65,34 @@ namespace LanguageFeatures.Controllers
decimal cartTotal = cart.TotalPrices();
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));
}
}
}

View File

@ -16,5 +16,27 @@ namespace LanguageFeatures.Models
}
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;
}
}
}
}
}

View File

@ -1,4 +1,4 @@

@Model String
@{
Layout = null;
}