Enumerable 类提供的表达式方法

LINQ方法详解
本文深入解析了LINQ中七种核心方法的使用:Aggregate、All、Any、Average、Find、ForEach及Select,通过实例展示了如何在C#中利用这些方法进行高效的数据处理和操作。

【01】

【名称】Aggregate<>

【描述】对应用序列累加函数

【用法】

ExpandedBlockStart.gif代码
 string sentence = "the quick brown fox jumps over the lazy dog";

 
string[] words = sentence.Split(' ');

 
string reversed = words.Aggregate((workingSentence, next) =>next + " " + workingSentence);

 Console.WriteLine(reversed);


/*
   传入一个起始值,也可以理解为已经累加完的值,然后下一个值
*/

 

 

【02】

【名称】All<>

【描述】确定序列中的所有元素是否满足条件

【用法】

 

ExpandedBlockStart.gif代码
List<UserClass> Lists = new List<UserClass>
            {
                
new UserClass{ Age=15, Name="先生A"},
                
new UserClass{ Age=16, Name="先生B"},
                
new UserClass{ Age=17, Name="女士A"},
                
new UserClass{ Age=22, Name="女士B"},
                
new UserClass{ Age=24, Name="女士C"},
                
new UserClass{ Age=26, Name="女士D"},
                
new UserClass{ Age=27, Name="女士E"},
                
new UserClass{ Age=28, Name="女士F"}
            };
            
 
bool _Default = Lists.All(User => { return User.Age > 50; });

/*
   结果为:false 所有的都不满足
*/

 

 

【03】

【名称】Any<>

【描述】确定序列中的任何元素是否都满足条件

【用法】

 

bool _Default =Lists.Any( u => { return u.Age > 20; } );//结果为:true,只要有一个满足条件就为true

 

 

【04】

【名称】Average<>

【描述】可以计算出序列当中的平均值

【用法】

 

double dou = Lists.Average(User => { return User.Age; });
//结果为 21.875

 

 

【05】

【名称】Find<>

【描述】查找集合当中指定的对象

【用法】 Lists.Find(U => { return U.Age == 22; });

 

 

【06】

【名称】ForEach

【描述】遍历集合中的对象

【用法】

 

Lists.ForEach(  U => { if (U.Age < 20) MessageBox.Show(U.Name); }  );

 

 

【07】

【名称】GroupBy<>

【描述】分组,这个方法有好多参数大家慢慢研究,我指出一个例子的用法

【用法】

 

ExpandedBlockStart.gif代码
List<UserClass> Lists = new List<UserClass>
            {
                
new UserClass{ Age=15, Name="先生A"},
                
new UserClass{ Age=15, Name="先生B"},
                
new UserClass{ Age=15, Name="女士A"},
                
new UserClass{ Age=16, Name="女士B"},
                
new UserClass{ Age=16, Name="女士C"},
                
new UserClass{ Age=20, Name="女士D"},
                
new UserClass{ Age=20, Name="女士E"},
                
new UserClass{ Age=20, Name="女士F"}
            };

            Lists.GroupBy(pet 
=> pet.Age,
                   (Age, pets) 
=> new
                   {
                       Key 
= Age,
                       Count 
= pets.Count(),
                       Min 
= pets.Min(pet => pet.Age),
                       Max 
= pets.Max(pet => pet.Age)
                   }
               ).ToList().ForEach(U 
=> Console.WriteLine("【总数:{0}】【最大值:{1}】【最小值:{2}】【分组:{3}】", U.Count, U.Max, U.Min, U.Key));

            Console.ReadKey();

 

 

【07】

【名称】Select<>

【描述】返回新的对象数组

【用法】

ExpandedBlockStart.gif代码
 Lists.Select(U => new
            {
                Age 
= U.Age,
                Name 
= U.Name + DateTime.Now.ToString("yy-mm-dd")
            }
            ).ToList().ForEach(U 
=> Console.WriteLine("【年龄:{0}】【名称:{1}】", U.Age, U.Name));

 

 

 

 

转载于:https://www.cnblogs.com/tongly/archive/2010/11/09/1872906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值