本文整理汇总了C#中ShoppingCart.Filter方法的典型用法代码示例。如果您正苦于以下问题:C# ShoppingCart.Filter方法的具体用法?C# ShoppingCart.Filter怎么用?C# ShoppingCart.Filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ShoppingCart
的用法示例。
在下文中一共展示了ShoppingCart.Filter方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
var cart = new ShoppingCart {
Products = new List<Product> {
new Product {Name="Pencil",Category="Stationery" },
new Product {Name="Tie",Category="Clothes" },
new Product {Name="Pen",Category="Stationery" }
}
};
Func<Product, bool> clothesFilter = delegate (Product product) {
return product.Category == "Clothes";
};
foreach (var product in cart.Filter(clothesFilter)) {
Console.Write(product.Name + ", ");
}
Console.WriteLine();
var stationeryItems = cart.Filter(delegate (Product product) {
return product.Category == "Stationery";
});
foreach (var product in stationeryItems) {
Console.Write(product.Name + ", ");
}
Console.WriteLine();
}
示例2: Main
static void Main(string[] args)
{
// create and populate ShoppingCart
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 = "Corner flag", Category = "Soccer", Price = 34.95M}
}
};
// create and populate an array of Product objects
Product[] productArray =
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
};
// get the total value of the products in the cart
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = products.TotalPrices();
Console.WriteLine("Cart Total: {0:c}", cartTotal);
Console.WriteLine("Array Total: {0:c}", arrayTotal);
Console.WriteLine(products.FilterByCategory("Soccer").TotalPrices());
Func<Product, bool> catagoryFilter = delegate(Product prod)
{
return prod.Category == "Soccer";
};
Console.WriteLine(products.Filter(catagoryFilter).TotalPrices());
foreach (Product prod in products.Filter(catagoryFilter))
{
Console.WriteLine("Name: {0}, Price: {1:c}", prod.Name, prod.Price);
}
Func<Product, bool> lambdaFilter = prod => prod.Category == "Soccer";
foreach (Product prod in products.Filter(lambdaFilter))
{
Console.WriteLine("Name: {0}, Price: {1:c}", prod.Name, prod.Price);
}
IEnumerable<Product> filteredProducts = products.Filter(prod => prod.Category == "Soccer");
}
示例3: Main
static void Main(string[] args)
{
// these statements are here so that we can generate
// results that display US currency values even though
// the locale of our machines is set to the UK
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
// create and populate ShoppingCart
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 = "Corner flag", Category = "Soccer", Price = 34.95M}
}
};
IEnumerable<Product> filteredProducts = products.Filter(prod =>
prod.Category == "Soccer" || prod.Price > 20);
foreach (Product prod in filteredProducts) {
Console.WriteLine("Name: {0}, Price: {1:c}", prod.Name, prod.Price);
}
}
示例4: UseFilterExtensionMethod2
//http://localhost:50591/Home/UseFilterExtensionMethod2
public ViewResult UseFilterExtensionMethod2()
{
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name="p1", Category="apple", Price=10 },
new Product {Name="p2", Category="samsung", Price=20 },
new Product {Name="p3", Category="apple", Price=30 }
}
};
//람다식
//Func<in, out> funcName = in => result(out)
//Func<Product, bool> categoryFilter = prod => prod.Category == "apple";
Func<Product, bool> categoryFilter = prod => (prod.Category == "apple" && prod.Price > 20);
decimal total = 0;
foreach(Product prod in products.Filter(categoryFilter))
{
total += prod.Price;
}
return View("Result", (object)String.Format("Total: {0}", total));
}
示例5: GetMessage1
public string GetMessage1()
{
#region 精通ASP.NET 49页之前的方法
IEnumerable<Product> products = new ShoppingCart { Products = new List<Product> { new Product { Name = "Young", Price = 245, Category = "watersports" }, new Product { Name = "wang", Price = 165, Category = "watersports" }, new Product { Name = "san", Price = 621, Category = "Soccer" } } };
Product[] productArray = { new Product { Name = "XING", Price = 120 }, new Product { Name = "EDASS", Price = 1120 }, new Product { Name = "XVEDD", Price = 120 } };
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
decimal yieldCartTotal = products.FilterByCategory("Soccer").TotalPrices(); ;
//过滤方法一
//Func<Product, bool> categoryFiler = delegate(Product prod)
//{
// return prod.Category == "Soccer";
//};
//过滤方法2 :lambda 替换委托定义
//Func<Product, bool> categoryFiler = prod => prod.Category == "Soccer";
//decimal total = products.Filter(categoryFiler).TotalPrices();
//过滤方法2 :lambda 没有Func
decimal total = products.Filter(prod => prod.Category == "Soccer" || prod.Price > 20).TotalPrices();
// return string.Format("Cart Total:{0},Array Total:{1},yield Soccer Total:{2},filer Total:{3}", cartTotal, arrayTotal, yieldCartTotal, total);
#endregion
//创建匿名类型
var myAnonType = new { Name = "YOUNG", Category = "Watersports" };
// return string.Format("Name:{0}, Type:{1}",myAnonType.Name,myAnonType.Category);
//使用匿名类型创建对象数组
var oddsAndEnds = new[] { new { Name = "blue", category = "color" },
new { Name = "hat", category = "clothing" },
new { Name = "apple", category = "fruit" } };
StringBuilder result = new StringBuilder();
foreach (var item in oddsAndEnds)
{
result.Append(item.Name).Append(" ");
}
return result.ToString();
}
示例6: UseFilterExtensionMethod
public ViewResult UseFilterExtensionMethod()
{
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 = "Corner flag", Category = "Soccer", Price = 34.95M } } };
//Func<Product, bool> categoryFilter = prod => prod.Category == "Soccer";
decimal total = 0;
foreach (Product prod in products.Filter(prod => prod.Category == "Soccer" || prod.Price > 20)) { total += prod.Price; }
return View("Result", (object)String.Format("Total: {0}", total));
}
示例7: UseFilterExtensionMethod
//Shows how to use an EXTENSION METHOD
public ViewResult UseFilterExtensionMethod()
{
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 = "Corner flag", Category = "Soccer", Price = 34.95M},
}
};
//get the total value of the products in the cart
decimal total = 0;
foreach ( Product prod in products.Filter( prod => prod.Category == "Soccer" || prod.Price > 20 ) ) {
total += prod.Price;
} //FilterByCategory is part of MyExtensionMethods class, NOT the ShoppingCart class
return View("Result",
(object)String.Format("Total: {0}", total));
}
示例8: UseFilterExtentionMethodFunc
public ViewResult UseFilterExtentionMethodFunc()
{
// create and populate Shoppingcart
ShoppingCart cart = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name = "Kayak",Price = 275M, Category = "Watersports" },
new Product {Name = "Lifejacket",Price = 48.95M, Category = "Watersports" },
new Product {Name = "Soccer ball",Price = 19.5M, Category = "Soccer" },
new Product {Name = "Corner flag",Price = 34.95M, Category = "Soccer" }
}
};
decimal total = 0;
foreach (Product prod in cart.Filter(prod => prod.Category == "Soccer" || prod.Price > 200))
{
total += prod.Price;
}
return View("Result", (object)String.Format("Total: {0:c}", total));
}
示例9: UseFilterExtensionMethod
public ViewResult UseFilterExtensionMethod()
{
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 = "Corner flag", Category = "Soccer", Price = 34.95M}
}
};
decimal total1 = products.FilterByCategory("Soccer").TotalPrices();
// Using Filter
// 1 - delegate
Func<Product, bool> categoryFilter1 = delegate(Product prod) {
return prod.Category == "Soccer";
};
decimal total2 = products.Filter(categoryFilter1).TotalPrices();
// 2 - Lambda Expression
Func<Product, bool> categoryFilter2 = prod => prod.Category == "Soccer";
decimal total3 = products.Filter(categoryFilter2).TotalPrices();
decimal total4 = products.Filter(prod => prod.Category == "Soccer").TotalPrices();
return View("Result", (object)String.Format("Total1: {0}, Total2: {1}, Total3: {2}, Total4: {3}", total1, total2, total3, total4));
}
示例10: UseFilterExtensionMethod
public ViewResult UseFilterExtensionMethod()
{
// create and populate ShoppingCart
ShoppingCart cart = new ShoppingCart {
Products = new List<Product> {
new Product {Name = "Kayak", Price = 275M, Category="Watersports"},
new Product {Name = "Lifejacket", Price = 48.95M, Category="Watersports"},
new Product {Name = "Soccer ball", Price = 19.50M, Category="Soccer" },
new Product {Name = "Corner flag", Price = 34.95M, Category="Soccer" }
}
};
Func<Product, bool> dCategoryFilter = delegate(Product prod) { return prod.Category == "Soccer"; };
Func<Product, bool> lCategoryFilter = prod => prod.Category == "Soccer";
//return View("Result", (object)string.Format("Total: {0:c}", cart.FilterByCategory("Soccer").TotalPrices()));
return View("Result", (object)string.Format("Total: {0:c}", cart.Filter(dCategoryFilter).TotalPrices()));
}
示例11: UseFilterExtensionMethod
/// <summary>
/// 필터링 확장 메서드 사용
/// </summary>
/// <returns></returns>
public ViewResult UseFilterExtensionMethod() {
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product> {
new Product {Name = "kim", Price = 100M, Category = "A" },
new Product {Name = "Lee", Price = 200M , Category = "B"},
new Product {Name = "Park", Price = 300M , Category = "A"},
new Product {Name = "Han", Price = 400M , Category = "C" }
}
};
//Func<Product, bool> categoryFilter = delegate (Product prod)
//{
// return prod.Category == "A";
//};
//Func<Product, bool> categoryFilter = prod => prod.Category == "A";
decimal total = 0;
//foreach (Product prod in products.FilterByCategory("A")) {
//foreach (Product prod in products.Filter(categoryFilter))
foreach (Product prod in products.Filter(prod => prod.Category == "A"))
{
total += prod.Price;
}
return View("Result", (object)String.Format("Total : {0}", total));
}
示例12: UseExtensionMethod
public ViewResult UseExtensionMethod()
{
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name="Kajak", Category = "Sporty wodne", Price=275M },
new Product {Name="Kamizelka ratunkowa",Category = "Sporty wodne", Price=48.95M },
new Product {Name="Piłka nożna", Category = "Piłka nożna", Price=19.50M },
new Product {Name="Flaga narożna",Category = "Piłka nożna", Price=34.95M },
}
};
// Func<Product, bool> categoryFilter = delegate(Product prod)
// {
// return prod.Category == "Piłka nożna";
// };
// Func<Product, bool> categoryFilter = prod => prod.Category == "Piłka nożna";
var total = products.Filter(prod => prod.Category == "Piłka nożna"||prod.Price>20).Sum(prod => prod.Price);
return View("Result", (object)String.Format("Razem: {0:c}", total));
}