LINQ 学习路程 -- 查询操作 Average Count Max Sum
IList<int> intList = new List<int>>() { , , }; var avg = intList.Average(); Console.WriteLine("Average: {0}", avg);
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var avgAge = studentList.Average(s => s.Age); Console.WriteLine("Average Age of Student: {0}", avgAge);
var totalAge = (from s in studentList
select s.age).Count();
int Count<TSource>(); int Count<TSource>(Func<TSource, bool> predicate);
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Mathew" , Age = }
}; var numOfStudents = studentList.Count(); Console.WriteLine("Number of Students: {0}", numOfStudents);
// Student collection
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Mathew" , Age = }
}; var numOfStudents = studentList.Count(s => s.Age >= ); Console.WriteLine("Number of Students: {0}", numOfStudents);
IList<int> intList = new List<int>() { , , , , , }; var largest = intList.Max(); Console.WriteLine("Largest Element: {0}", largest); var largestEvenElements = intList.Max(i => {
if(i% == )
return i; return ;
}); Console.WriteLine("Largest Even Element: {0}", largestEvenElements );
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var oldest = studentList.Max(s => s.Age); Console.WriteLine("Oldest Student Age: {0}", oldest);
public class Student : IComparable<Student>
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
public int StandardID { get; set; } public int CompareTo(Student other)
{
if (this.StudentName.Length >= other.StudentName.Length)
return ; return ;
}
}
class Program
{
static void Main(string[] args)
{
// Student collection
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Steve" , Age = 15 }
};
var studentWithLongName = studentList.Max();
Console.WriteLine("Student ID: {0}, Student Name: {1}",
.StudentID, studentWithLongName.StudentName)
}
}
IList<int> intList = new List<int>() { , , , , , }; var total = intList.Sum(); Console.WriteLine("Sum: {0}", total); var sumOfEvenElements = intList.Sum(i => {
if(i% == )
return i; return ;
}); Console.WriteLine("Sum of Even Elements: {0}", sumOfEvenElements );
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var sumOfAge = studentList.Sum(s => s.Age); Console.WriteLine("Sum of all student's age: {0}", sumOfAge); var numOfAdults = studentList.Sum(s => { if(s.Age >= )
return ;
else
return ;
}); Console.WriteLine("Total Adult Students: {0}", numOfAdults);
LINQ 学习路程 -- 查询操作 Average Count Max Sum的更多相关文章
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 ElementAt, ElementAtOrDefault
Element Operators (Methods) Description ElementAt 返回指定索引的元素,如果索引超过集合长度,则抛出异常 ElementAtOrDefault 返回指定 ...
随机推荐
- lower_bound() 与 upper_bound()
1. lower_bound() lower_bound()是泛型算法,在使用时,需要先将序列进行排序: 作用: 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返 ...
- IMSDroid问题集
1.IMSDroid切换摄像头后的接收方横屏显示.事实上非常多种情况下都会突然发现就横屏了.解决的方法就是切换摄像头时同一时候切换横竖屏显示 2.IMSDroid掉音问题:IMSDroid通话几分钟后 ...
- rbg大神的主页
http://www.rossgirshick.info/ Ross Girshick (rbg)Research ScientistFacebook AI Research (FAIR) r...@ ...
- linux内核中mtd架构分析
一. 引言 MTD(memory technology device内存技术设备)是用于访问memory设备(RAM.ROM.flash)的Linux的子系统.MTD的主要目的是为了使新的memory ...
- Java异常封装(自定义错误码和描写叙述,附源代码)
真正工作了才发现.Java里面的异常在真正工作中使用还是十分普遍的. 什么时候该抛出什么异常,这个是必须知道的. 当然真正工作里面主动抛出的异常都是经过分装过的,自己能够定义错误码和异常描写叙述. 以 ...
- pip安装错误,用镜像
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'Connec ...
- win10正式版开始菜单无法打开,右边的网络连接、操作中心也打不开
问题描述: 开机后电脑键盘的win键无响应,鼠标点击菜单栏中的这几个按键也都无响应,但是点击自己固定的应用程序却没有问题,在网上查找尝试了许多资料,终于找到了一个合适的解决方案.现记录如下 解决方案: ...
- Node-Webkit - package.json - 参数设置
必填: main :(string)APP的主入口,指定一个html文件,如:main:"index.htm". name :(string)APP的名称,必须具有唯一性. 例子: ...
- fzu2020( c(n,m)%p,其中n, m, p (1 <= m <= n <= 10^9, m <= 10^4, m < p < 10^9, p是素数) )
基本的模板题,统计分子分母中p出现的次数,然后求逆元取模. // // main.cpp // fzu2020 // // Created by 陈加寿 on 15/12/27. // Copyrig ...
- ArcGIS API for js InfoWindow
说明:有关该示例中怎么引用部署在iis上的离线arcgis api请参考我前面的博文 1.运行效果 2.HTML代码 <!DOCTYPE html> <html> <he ...