本文整理汇总了C#中ShoppingCart.FilterByCategory方法的典型用法代码示例。如果您正苦于以下问题:C# ShoppingCart.FilterByCategory方法的具体用法?C# ShoppingCart.FilterByCategory怎么用?C# ShoppingCart.FilterByCategory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ShoppingCart
的用法示例。
在下文中一共展示了ShoppingCart.FilterByCategory方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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");
}
示例2: 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" }
}
};
foreach (var product in cart.FilterByCategory("Stationery")) {
Console.Write(product.Name + ", ");
}
Console.WriteLine();
}
示例3: UseFilterExtensionMethod
//http://localhost:50591/Home/UseFilterExtensionMethod
public ViewResult UseFilterExtensionMethod()
{
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 }
}
};
//
decimal total = 0;
foreach(Product prod in products.FilterByCategory("apple"))
{
total += prod.Price;
}
return View("Result", (object)String.Format("Total: {0}", total));
}
示例4: 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();
}
示例5: 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}
}
};
//// 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}
//};
decimal total = products.FilterByCategory("Soccer").TotalPrices();
Console.WriteLine("Filtered total: {0:c}", total);
// 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);
}
示例6: UseFilterExtentionMethod
public ViewResult UseFilterExtentionMethod()
{
// 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 cartTotal = cart.FilterByCategory("Soccer").Totalprices();
return View("Result", (object)String.Format("Total: {0:c}", cartTotal));
}
示例7: 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));
}
示例8: 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 total = 0;
foreach (Product prod in products.FilterByCategory("Soccer"))
{
total += prod.Price;
}
return View("Result", (object)String.Format("total: {0}", total));
}
示例9: UseExtensionEnumerable
public ViewResult UseExtensionEnumerable()
{
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 },
}
};
var total = products.FilterByCategory("Piłka nożna").Sum(prod => prod.Price);
return View("Result", (object)String.Format("Razem: {0:c}", total));
}