本文共 1471 字,大约阅读时间需要 4 分钟。
using System;using System.Linq;namespace ConsoleApplication21{ public class Product { public int ProductID { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string Category { set; get; } } class Program { static void Main(string[] args) { Product[] products = { new Product {Name = "西瓜", Category = "水果", Price = 2.3M}, new Product {Name = "苹果", Category = "水果", Price = 4.9M}, new Product {Name = "空心菜", Category = "蔬菜", Price = 2.2M}, new Product {Name = "地瓜", Category = "蔬菜", Price = 1.9M} }; var results = from product in products orderby product.Price descending select new { product.Name, product.Price }; var res2 = products.Where(r=>r.Name=="地").Count(); Console.WriteLine(res2); var res1 = products.Single(g => g.Name == "地瓜"); Console.WriteLine("商品:{0},价钱:{1}", res1.Name, res1.Price); var res = products.Sum(p => p.Price); /* //打印价钱最高的三个商品 int count = 0; foreach (var p in results) { Console.WriteLine("商品:{0},价钱:{1}", p.Name, p.Price); if (++count == 3) break; } Console.ReadKey(); */ } }}
转载地址:http://ttdaf.baihongyu.com/