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 ...
随机推荐
- oracle 启动模式
转载自:http://blog.csdn.net/nsj820/article/details/6573525 <一>.ORACLE数据库启动模式 1.启动SQL*PLUS不与数据库连接 ...
- 【ask】webstorm调试node单个js文件
The procedure falls into two parts: first we start an application as usual and then connect to it wi ...
- sourcenav安装
$ ./configure之后会出现 configure: error: ./configure failed for unixconfigure: error: ./configure failed ...
- iOS QQ的AppID转换16进制的方法
801312852为QQ的AppID 打开终端 echo 'ibase=10;obase=16;801312852'|bc 红色框内的即为转换后的16进制
- pyhon多进程知识整理
多进程概要: 在Unix/Linux下,可以使用fork()调用实现多进程. 要实现跨平台的多进程,可以使用multiprocessing模块. 进程间通信是通过Queue.Pipes等实现的. 要让 ...
- aspx.cs方法设置webmenthod特性接收ajax请求
cs代码: public partial class TelerikWebMethod : BasePage//System.Web.UI.Page { protected void Page_Loa ...
- Android无线测试之—Genymotion配置过程中常见问题
一.前提条件: 已经部署好了Android UiAutomator测试环境. 二.在部署Genymotion时遇到了两类问题: 1.通过eclipse打开一个模拟设备,然后将编译好的jar包push到 ...
- python3使用SMTP发送邮件
环境:python3 ,IDE : pycharm 非常奇怪的是,用163发送邮件,如果电脑连校园网发送,会被当成垃圾邮件拒绝 如果用手机开热点就可以正常发送 代码如下 #!/usr/bin/pyth ...
- EasyNVR智能云终端硬件使用场景分析:如何实现软硬一体的视频上云整体解决方案
背景分析 在于众多的客户交流中,经常会被客户问到,"EasyNVR到底是软件还是硬件?"."EasyNVR能否出一个硬件的版本,摆脱自建服务器的压力?".&qu ...
- VS2012如何显示行号
Tools-Options-Text Editor-All Languages –General – Display