本文整理汇总了C#中ShoppingCart.TotalPrices方法的典型用法代码示例。如果您正苦于以下问题:C# ShoppingCart.TotalPrices方法的具体用法?C# ShoppingCart.TotalPrices怎么用?C# ShoppingCart.TotalPrices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ShoppingCart
的用法示例。
在下文中一共展示了ShoppingCart.TotalPrices方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: UseExtensionEnumerable
//http://localhost:50070/Home/UseExtensionEnumerable
public ViewResult UseExtensionEnumerable()
{
//A Type
IEnumerable<Product> products = new ShoppingCart {
Products = new List<Product>
{
new Product { Name="p1", Price=10 },
new Product { Name="p2", Price=20 },
new Product { Name="p3", Price=30 }
}
};
//B Type (배열도 IEnumerable 인터페이스를 구현하고 있다)
Product[] productArray =
{
new Product { Name="p1", Price=10 },
new Product { Name="P2", Price=20 },
new Product { Name="p3", Price=30 }
};
//Total
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
return View("Result", (object)String.Format("Cart Total : {0}, Array Total: {1}", cartTotal, arrayTotal));
}
示例3: UseExtensionEnumerable
public ViewResult UseExtensionEnumerable()
{
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>
{
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}
}
};
//Создать и заполнить массив обьектов Product
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}
};
// Получить общую стоимость товаров в корзине
var cartTotal = products.TotalPrices();
var arrayTotal = productArray.TotalPrices();
return View("Result",
(object) string.Format("CartTotal: {0:c}, ArrayTotal: {1:c}"
, cartTotal, arrayTotal));
}
示例4: UseExtension
/// <summary>
/// 확장메서드 사용!
/// </summary>
/// <returns></returns>
public ViewResult UseExtension() {
ShoppingCart cart = new ShoppingCart
{
Products = new List<Product> {
new Product {Name = "kim", Price = 200M },
new Product {Name = "Lee", Price = 200M },
new Product {Name = "Park", Price = 200M },
new Product {Name = "Han", Price = 200M }
}
};
decimal cartTotal = cart.TotalPrices();
return View("Result", (object)String.Format("Total: {0:c}", cartTotal));
}
示例5: CreateCollection
public ViewResult CreateCollection()
{
ShoppingCart cart = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name="Kajak", Price=275M },
new Product {Name="Kamizelka ratunkowa", Price=48.95M },
new Product {Name="Piłka nożna", Price=19.50M },
new Product {Name="Flaga narożna", Price=34.95M },
}
};
var cartTotal = cart.TotalPrices();
return View("Result", (object)String.Format("Razem: {0:c}", cartTotal));
}
示例6: UseExtension
public ViewResult UseExtension() {
// create and populate ShoppingCart
ShoppingCart cart = new ShoppingCart {
Products = new List<Product> {
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 = cart.TotalPrices();
return View("Result",
(object)String.Format("Total: {0:c}", cartTotal));
}
示例7: 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();
}
示例8: UseExensionEnumerable
public ViewResult UseExensionEnumerable()
{
IEnumerable<Product> products = new ShoppingCart() { Products = new List<Product>() { 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 } } };
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 cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
return View("Result", (object)String.Format("Cart Total: {0}, ArrayTotal: {1}", cartTotal, arrayTotal));
}
示例9: UseExtensionEnumerable
public ViewResult UseExtensionEnumerable() {
IEnumerable<Product> products = new ShoppingCart {
Products = new List<Product> {
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}
}
};
// 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();
return View("Result",
(object)String.Format("Cart Total: {0}, Array Total: {1}",
cartTotal, arrayTotal));
}
示例10: Main
static void Main(string[] args)
{
Console.WriteLine("Beginning...");
Product demoProduct = Product.GetDemoProduct();
Console.WriteLine("Demo Product");
Console.WriteLine(string.Format("Category: {0}", demoProduct.Category));
Console.WriteLine("---");
Console.WriteLine("Demo Extension Method for ShoppingCart");
ShoppingCart cart = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name = "Kayak", Price = 277M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer Ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M},
}
};
decimal cartTotal = cart.TotalPrices();
Console.WriteLine(string.Format("cart.TotalPrices(): ${0}", cartTotal));
Console.WriteLine("---");
Console.WriteLine("Demo Extension Method to an Interface for IEnumerable<Product>");
IEnumerable<Product> products = new ShoppingCartForProducts
{
Products = new List<Product>
{
new Product {Name = "Kayak", Category = "Watersports", Price = 277M},
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},
}
};
// also test out an array of products (which should impliment ienumerable
Product[] productArray =
{
new Product {Name = "Kayak", Price = 277M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer Ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M},
};
Console.WriteLine(string.Format("IEnumerable<product>.TotalPrices(): {0},\n\r Product[].TotalPrices(): ${1}", products.TotalPrices(), productArray.TotalPrices()));
Console.WriteLine("---");
Console.WriteLine("Demo Filtering Extension Methods (combined with previous)");
decimal filteredTotal = 0;
foreach (var prod in products.FilterOnlyByCategory("Watersports"))
{
filteredTotal += prod.Price;
}
Console.WriteLine(string.Format("Price of products.FilterByCategory(\"Watersports\").TotalPrices():\n\r ${0}", products.FilterOnlyByCategory("Watersports").TotalPrices()));
Console.WriteLine(string.Format("Price of products.FilterByCategory(\"Soccer\").TotalPrices():\n\r ${0}", products.FilterOnlyByCategory("Soccer").TotalPrices()));
Console.WriteLine(string.Format("Price of products.FilterByCategory(\"Made Up Category\").TotalPrices():\n\r ${0}", products.FilterOnlyByCategory("Made Up Category").TotalPrices()));
Console.WriteLine("---");
Console.WriteLine("Demo Product Filtering Using Anon Func<> delegate with Anon Method ");
// Func<parmType,returnType> replaces a class TYPE, because it defines what will be any anonymous method that accpets and parmType and returns a returnType
// using an anonymous method that must return bool
Func<Product, bool> categoryDelegateFilter = delegate(Product prod)
{
return prod.Category == "Soccer";
};
decimal delegateFilteredTotal = 0;
foreach (var prod in products.Filter(categoryDelegateFilter))
{
delegateFilteredTotal += prod.Price;
}
Console.WriteLine(string.Format("Total: {0}", delegateFilteredTotal));
Console.WriteLine("---");
Console.WriteLine("Demo Product Filtering Using Lamda Func delegate");
// could also use lamda, since you can tecnically pass in a parameter
// the => means 'as a passed parameter'
Func<Product, bool> categoryLamdaFilter = something => something.Category == "Watersports";
decimal lamdaFilteredTotal = 0;
foreach (var prod in products.Filter(categoryLamdaFilter))
{
lamdaFilteredTotal += prod.Price;
}
Console.WriteLine(string.Format("Total: {0}", lamdaFilteredTotal));
Console.WriteLine("---");
Console.ReadKey();
}
示例11: GetMessage12
/// <summary>
/// 《精通asp.net4.5》书40页之前的内容
/// </summary>
/// <returns></returns>
protected string GetMessage12()
{
#region 构建和初始化包含属性的对象
//Product myProduct = new Product();
//myProduct.Name = "Young";
//myProduct.ProductID = 100;
//myProduct.Description = "a BOAT FOR one person";
//myProduct.Price = 247;
//myProduct.Category = "watersports";
#endregion 构建和初始化包含属性的对象
#region //使用对象初始化器功能
//Product myProduct1 = new Product { Name = "YOUNG1", Category = "hotsports", Price = 120, ProductID = 525 };
// return string.Format("Product name:{0}", myProduct1.Category);
#endregion //使用对象初始化器功能
#region 对集合和数组进行初始化
//string[] stringArray = { "apple", "orange", "plum" };
//List<int> intList = new List<int> { 10, 20, 30, 40 };
//Dictionary<string, int> myDict = new Dictionary<string, int> { { "apple", 10 }, { "orange", 20 }, { "plum", 30 } };
//return string.Format("{0}", intList[2]);
#endregion 对集合和数组进行初始化
#region 应用拓展方法
//已一个集合的形式传递给shoppingcart
List<Product> lisr = new List<Product>();
lisr.Add(new Product { Name = "Tom,", Price = 125 });
lisr.Add(new Product { Name = "Tom,", Price = 125 });
lisr.Add(new Product { Name = "Tom,", Price = 125 });
ShoppingCart cart11 = new ShoppingCart();
cart11.Products = lisr;
ShoppingCart cart = new ShoppingCart { Products = new List<Product> { new Product { Name = "Tom,", Price = 125 }, new Product { Name = "Kayak", Price = 256 }, new Product { Name = "Young", Price = 120 } } };
decimal cartTotal = cart.TotalPrices();
return string.Format("Total:{0}", cartTotal);
#endregion 应用拓展方法
}
示例12: UseExtension
public ViewResult UseExtension()
{
ShoppingCart cart = new ShoppingCart()
{
Products = new List<Product>()
{
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=39.95M },
}
};
decimal cartTotal = cart.TotalPrices();
return View("Result", (object)String.Format("Total: {0:c}", cartTotal));
}
示例13: UseExtensionEnumerable
/// <summary>
/// 인터페이스에 확장메서드 적용
/// </summary>
/// <returns></returns>
public ViewResult UseExtensionEnumerable() {
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product> {
new Product {Name = "kim", Price = 200M },
new Product {Name = "Lee", Price = 200M },
new Product {Name = "Park", Price = 200M },
new Product {Name = "Han", Price = 200M }
}
};
Product[] productArray = {
new Product {Name = "kim", Price = 200M },
new Product {Name = "Lee", Price = 200M },
new Product {Name = "Park", Price = 200M },
new Product {Name = "Han", Price = 200M }
};
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
return View("Result", (object)String.Format("Cart Total: {0}, Array Total: {1}", cartTotal, arrayTotal));
}