IEnumerable的一些基本方法
在说明用法之后,先要弄点数据。
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; }
}
List<Product> products = new List<Product> {
new Product { ID=, Name="路易十八比萨饼", Region="意大利", Price=, IsFavorite = false },
new Product { ID=, Name="澳洲胡桃", Region="澳洲", Price=, IsFavorite = false },
new Product { ID=, Name="Almas鱼子酱", Region="伊朗", Price=, IsFavorite = false },
new Product { ID=, Name="和牛肉", Region="日本", Price=, IsFavorite = true },
new Product { ID=, Name="麝香猫咖啡豆", Region="印尼", Price=, IsFavorite = true },
new Product { ID=, Name="大红袍茶叶", Region="中国", Price=, IsFavorite = true },
new Product { ID=, Name="Kona Nigari矿泉水", Region="美国", Price=, IsFavorite = true },
new Product { ID=, Name="Diva伏特加", Region="北欧", Price=, IsFavorite = false },
new Product { ID=, Name="番红花的雄蕊", Region="地中海", Price=, IsFavorite = false },
};
一、ALL和ANY
bool allChina = products.All(p => p.Region == "中国");//所有项Region都要是中国,结果:False
bool anyChina = products.Any(p => p.Region == "中国");//某一项Region是中国,结果:True
二、聚集
int countIdGreater5 = products.Count(p => p.ID > );//ID大于5的记录数,结果:4
decimal maxPrice = products.Max(p => p.Price);//金额最高,结果:208000
int minId = products.Min(p => p.ID);//编号最小,结果:1
decimal avgPrice = products.Average(p => p.Price);//金额平均值,结果:53538
decimal sumPrice = products.Sum(p => p.Price);//金额总值 结果:481842
三、累加器
Product aggregate1 = products.Aggregate((total, next) =>//累加器,对products中每一个元素执行一次Func
{
total.Price += next.Price;
return total;
});
上面的代码可以做一下优化
decimal aggregate2 = products.Aggregate(2000M, (total, next) =>//累加器可以给初始值,这里给的值是2000
{
total += next.Price;
return total;//这里返回的类型和初始值一致
});
累加器操作的时候尽量用值类型,上面2段代码如果一起执行,aggregate2的值就会出现异常。
四、SELECT
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();//选择多列,可以转换成对象
//键值对必须要保证键值是唯一的,在键值不唯一的情况可以使用ToLookup方法
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));
});
五、ORDER BY
var rightOrder = products.OrderBy(p => p.IsFavorite).ThenByDescending(p => p.ID).ToList();//主IsFavorite,次ID
var errorOrder = products.OrderBy(p => p.IsFavorite).OrderByDescending(p => p.ID).ToList();//主ID,次IsFavorite
六、GROUP BY
var group = products.GroupBy(p => p.IsFavorite).Select(g => new { IsFavorite = g.Key, SumPrice = g.Sum(item => item.Price), CountItem = g.Count() }).ToList();
当然在写拉姆达表达式的时候,也顺便说一个LINQ的用法
var groupLinq = (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();
七、WHERE
List<Product> distinct = products.Distinct().ToList();//去掉重复的记录
List<Product> take = products.Take().ToList();//顺序取3条记录
List<Product> takeWhile = products.TakeWhile(p => p.ID <= ).ToList();//只要不满足条件了,返回所有当前记录
List<Product> skip = products.Skip().ToList();//顺序跳过3条记录
List<Product> skipWhile = products.SkipWhile(p => p.Price < ).ToList();//只要不满足条件了,返回所有剩余记录
List<Product> contains = products.Where(p => p.Name.Contains("红")).ToList();//包含“红”的集合
Product first = products.Where(p => p.Name.StartsWith("大")).First();//“大”开头的第一条记录 如果无记录,直接报异常
Product lastDefault = products.Where(p => p.Name.EndsWith("胡")).LastOrDefault();//“胡”结尾的最后一条记录 如果无记录,返回默认值(对象返回null)不会报异常
Product single = products.Where(p => p.ID == ).SingleOrDefault();//取单条记录,有多条时会报异常
Product elementDefault = products.ElementAtOrDefault();//返回第10条记录 如果没有第10条记录,返回默认值(对象返回null)不会报异常
八、默认
products.DefaultIfEmpty(new Product { ID = , Name = "默认产品", Region = "默认地区", Price = });//判断是否为空,是返回默认值,否返回products
单集合操作讲得差不多了,下面说一下多集合操作的,还是老套路,先弄点数据,这里我们数据用最普遍的DataTable格式
DataTable table1 = new DataTable();
table1.Columns.Add("ID");
table1.Columns.Add("Name");
table1.Columns.Add("Amount");
table1.Columns.Add("Description"); table1.Rows.Add("", "张三", "", "不知道和张三丰有什么关系?");
table1.Rows.Add("", "李四", "", "无");
table1.Rows.Add("", "王五", "", "是住你家隔壁的那位吗?"); DataTable table2 = new DataTable();
table2.Columns.Add("ID");
table2.Columns.Add("Name");
table2.Columns.Add("Amount");
table2.Columns.Add("Description");
table2.Rows.Add("", "张三", "", "不知道和张三丰有什么关系?");
table2.Rows.Add("", "老王", "", "这才是隔壁那位吧");
table2.Rows.Add("", "老刘", "", "无");
九、JOIN
//两表内联,结果有2条记录
var joinTable = table1.AsEnumerable().Join(table2.AsEnumerable(),
left => left["ID"].ToString(),
right => right["ID"].ToString(),
(left, right) => new {
LeftID = left["ID"].ToString(),
RightID = right["ID"].ToString(),
LeftName = left["Name"].ToString(),
RightName = right["Name"].ToString() }).ToList();
joinTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t.LeftID, t.RightID, t.LeftName, t.RightName));
十、GROUPJOIN
//以第一个表为基准,对第二个表进行分组
var groupJoinTable = table1.AsEnumerable().GroupJoin(table2.AsEnumerable(),
left => left["Description"].ToString(),
right => right["Description"].ToString(),
(key, g) => new {
Key = key["Description"].ToString(),
Count = g.Count(),
TotalAmount = g.Where(s => decimal.Parse(s["Amount"].ToString()) > ).Sum(s => decimal.Parse(s["Amount"].ToString()))
}).ToList();
groupJoinTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}", t.Key, t.Count, t.TotalAmount));
这里的统计不会包括第一个表的记录,第一个表只何为一个索引使用
十一、比较两个表是否相等
bool isEqual = table1.AsEnumerable().Where(t => t["ID"].ToString() == "")
.SequenceEqual(table2.AsEnumerable().Where(t => t["ID"].ToString() == ""), DataRowComparer.Default);
Console.WriteLine(isEqual);
这里只是做了单条记录的比较,为的只是返回一个TRUE,整个集合比较也是可以的
十二、连接两个表,不去重复,列取公共部分
var concatTable = table1.AsEnumerable().Concat(table2.AsEnumerable()).ToList();
concatTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"]));
十三、差集、交集、并集
//两表的差集
var exceptTable = table1.AsEnumerable().Except(table2.AsEnumerable(), DataRowComparer.Default).ToList();
exceptTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"])); //两表的交集
var intersectTable = table1.AsEnumerable().Intersect(table2.AsEnumerable(), DataRowComparer.Default).ToList();
intersectTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"])); //两表的并集
var unionTable = table1.AsEnumerable().Union(table2.AsEnumerable(), DataRowComparer.Default).ToList();
unionTable.ForEach(t => Console.WriteLine("{0}\t{1}\t{2}\t{3}", t["ID"], t["Name"], t["Amount"], t["Description"]));
最后说一个比较好玩的方法,还是老规矩,先弄数据
List<Store> stores = new List<Store>
{
new Store
{
ID = ,
Name = "城北",
Products = new List<Product> {
new Product { ID=, Name="路易十八比萨饼", Region="意大利", Price=, IsFavorite = false },
new Product { ID=, Name="澳洲胡桃", Region="澳洲", Price=, IsFavorite = false },
new Product { ID=, Name="Almas鱼子酱", Region="伊朗", Price=, IsFavorite = false }
}
},
new Store
{
ID = ,
Name = "城南",
Products = new List<Product> {
new Product { ID=, Name="和牛肉", Region="日本", Price=, IsFavorite = true },
new Product { ID=, Name="麝香猫咖啡豆", Region="印尼", Price=, IsFavorite = true },
new Product { ID=, Name="大红袍茶叶", Region="中国", Price=, IsFavorite = true }
}
},
new Store
{
ID = ,
Name = "城东",
Products = new List<Product> {
new Product { ID=, Name="Kona Nigari矿泉水", Region="美国", Price=, IsFavorite = true },
new Product { ID=, Name="Diva伏特加", Region="北欧", Price=, IsFavorite = false },
new Product { ID=, Name="番红花的雄蕊", Region="地中海", Price=, IsFavorite = false }
}
}
};
我把上面的9个产品分到了3个仓库里面存在,当我要查找金额小于10000的所有产品时,按以前的做法就要写2个FOREACH循环,现在用到SELECTMANY就方便多了
var selectMany = stores.SelectMany(s => s.Products).Where(p => p.Price < ).ToList();
selectMany.ForEach(item => Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", item.ID, item.Name, item.Region, item.Price));
当然,也可以用LINQ的方式
var linqSelectMany = from s in stores
from p in s.Products
where p.Price <
select p;
linqSelectMany.ToList().ForEach(item => Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", item.ID, item.Name, item.Region, item.Price));
IEnumerable的一些基本方法的更多相关文章
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- 为IEnumerable扩展一个ForEach方法
IEnumerable没有一个ForEach方法,我们可以使用C#写一个扩展方法: Source Code: using System; using System.Collections.Generi ...
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
- C#对IQueryable<T>、IEnumerable<T>的扩展方法
#region IQueryable<T>的扩展方法 #region 根据第三方条件是否为真是否执行指定条件的查询 /// <summary> /// 根据第三方条件是否为真是 ...
- IEnumerable接口的Aggregate方法
以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码: string result ="": foreach (var item in items) { result+=i ...
- IEnumerable中的 Any方法
IEnumerable类中的 Any方法,表示集合中有任何一元素满足条件,返回就true , 该方法有两个重载 1. 不带任何参数,表示集合中有元素 2. 参入一个 Func<TSource, ...
- 为IEnumerable类型添加Add方法
IEnumerable类型原生是没有Add方法的,你可以用Contact方法去为它添加元素, 1 items = items.Concat(new[] { "foo" }); 也可 ...
- IEnumerable的一些基本方法 补充
接上一篇,我们发现两表连接方式默认为内连接,而我们在SQL中常用到的左连接没有封装方法.换句话说,微软放弃两表左连或右连的这种做法(只有在2个表都存在值时,这样的连接才有意义). 如果要实现表的左连接 ...
- IEnumerable对象的Distinct方法重写
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource ...
随机推荐
- Problem 500!!! (Project Euler 500)
题目大意:求出最小的正整数,它的约数有$2^{500500}$个. 思路:考虑将一个数质因数分解,如果它的约数有$2^{500500}$个, 那么每个质因子的指数一定是$2^k-1$这样的形式. 如果 ...
- 微信小程序 缓存说明
每个微信小程序都可以有自己的本地缓存,可以通过 wx.setStorage(wx.setStorageSync).wx.getStorage(wx.getStorageSync).wx.clearSt ...
- 求伪逆矩阵c++代码(Eigen库)
非方阵的矩阵的逆矩阵 pseudoInverse 伪逆矩阵是逆矩阵的广义形式,广义逆矩阵 matlab中是pinv(A)-->inv(A). #include "stdafx.h&q ...
- Mac下使用Homebrew 安装MySQL
安装 brew install mysql 卸载 brew uninstall mysql 启动mysql mysql.server start 管理员账户 mysql -uroot
- linux下不错的小软件
1.Shutter截图软件 可以完成基本截图功能,而且还有图片编辑功能,可以涂鸦添加水印等. 以下的截图全部归功于shutter软件. 2.VLC media player 媒体播放器 3.Termi ...
- 【Python之路】第十三篇--DOM
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- centos7 Mysql5.6 升级Mysql5.7
1 2. 卸载Mysql5.6 ,一共有三个包 要卸载: (1)先卸载mysql-server包 : 执行命令 yum remove mysql mysql-server (2)再卸载mysql-c ...
- sql语句备份/导入 mysql数据库或表命令
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq1355541448/article/details/30049851
- 《Python 机器学习》笔记(一)
赋予计算机学习数据的能力 涵盖: 1.机器学习的一般概念 2.机器学习方法的三种类型和基本术语 3.成功构建机器学习系统所需的模块 机器学习的三种不同方法 1.监督学习 2.无监督学习 3.强化学习 ...
- SpringBoot入门1—简介及helloworld
Spring Boot简介 Spring Boot让我们的Spring应用变的更轻量化.比如:你可以仅仅依靠一个Java类来运行一个Spring引用.你也可以打包你的应用为jar并通过使用java - ...