class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Region { get; set; }
public decimal Price { get; set; }
public bool IsFavorite { get; set; }
}
void testIEnumerable()
{
List<Product> products = new List<Product> {
new Product { ID=1, Name="路易十八比萨饼", Region="意大利", Price=79961, IsFavorite = false },
new Product { ID=2, Name="澳洲胡桃", Region="澳洲", Price=195, IsFavorite = false },
new Product { ID=3, Name="Almas鱼子酱", Region="伊朗", Price=129950, IsFavorite = false },
new Product { ID=4, Name="和牛肉", Region="日本", Price=3250, IsFavorite = true },
new Product { ID=5, Name="麝香猫咖啡豆", Region="印尼", Price=2000, IsFavorite = true },
new Product { ID=6, Name="大红袍茶叶", Region="中国", Price=208000, IsFavorite = true },
new Product { ID=7, Name="Kona Nigari矿泉水", Region="美国", Price=13000, IsFavorite = true },
new Product { ID=8, Name="Diva伏特加", Region="北欧", Price=6500, IsFavorite = false },
new Product { ID=9, Name="番红花的雄蕊", Region="地中海", Price=38986, IsFavorite = false },
};
bool allChina = products.All(p => p.Region == "中国");
bool anyChina = products.Any(p => p.Region == "中国");
int count = products.Count(p => p.ID > 5);
decimal max = products.Max(p => p.Price);
int minid = products.Min(p => p.ID);
decimal avg = products.Average(p => p.Price);
decimal sum = products.Sum(p => p.Price);
Product agg = products.Aggregate((total, next) => { total.Price += next.Price; return total; });
string[] select1 = products.Select(p => p.Name).ToArray();
var select2 = products.Select(p => new { p.ID, p.Name }).ToDictionary(d => d.ID);
var selectMore = products.Select(p => new { p.ID, p.Name, p.Price }).ToList();
var lookup = products.ToLookup(l => l.IsFavorite, p => new { p.ID, p.Name, p.Region, p.Price }).ToList();
lookup.ForEach(l =>
{
Console.WriteLine(l.Key ? "己收" : "未收");
l.ToList().ForEach(item => Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", item.ID, item.Name, item.Region, item.Price));
});
var rightorder = products.OrderBy(p => p.IsFavorite).ThenByDescending(p => p.ID).ToList();
var group = products.GroupBy(p => p.IsFavorite).Select(g => new { IsFavorite = g.Key, SumPrice = g.Sum(item => item.Price), CountItem = g.Count() }).ToList();
group.ForEach(g => Console.WriteLine("{0}\t{1}\t{2}", g.IsFavorite, g.SumPrice, g.CountItem));
//Linq语法
var grouping = (from p in products
group p by p.IsFavorite
into g
select new { IsFavorite = g.Key, SumPrice = g.Sum(item => item.Price), CountItem = g.Count() }
).ToList();
var ps = (from p in products where p.ID > 4 select new { ID = p.ID, Name = p.Name, Price = p.Price }).ToList();
foreach (var item in ps)
{
Console.WriteLine("\t{0}\t{1}\t{2}", item.ID, item.Name, item.Price);
}
var distinct = products.Distinct().ToList();//去重
var take = products.Take(3).ToList(); //从头取3条记录
var takewhile = products.TakeWhile(p => p.ID <= 4).ToList(); //从头取记录,满足条件继续,遇到不满足条件停止
var skip = products.Skip(3).ToList(); //跳过开头3条记录
var skipwhile = products.SkipWhile(p => p.Price < 10000).ToList(); //当满足条件就跳过,遇到不满足条件就取剩下所有记录
var contains = products.Where(p => p.Name.Contains("红")).ToList(); //Name中包含”红"字
var first = products.Where(p => p.Name.StartsWith("大")).First(); //Name大开头的记录的第一条。无记录报异常
}