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 返回指定 ...
随机推荐
- 连接redis失败,关闭防火墙即可
因为linux上有防火墙,我用redis desktop manager 测试所以始终连接不上, 关闭防火墙: systemctl stop firewalld.service #停止firewall ...
- python 的三元表达式
python中的三目运算符不像其他语言 其他的一般都是 判定条件?为真时的结果:为假时的结果 如 result=5>3?1:0 这个输出1,但没有什么意义,仅仅是一个例子. 而在python中的 ...
- oracle海量数据中提升创建索引的速度
基本信息情况: 数据库版本:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production 操作系统版本:Ce ...
- HDFS源码分析之FSImage文件内容(一)总体格式
FSImage文件是HDFS中名字节点NameNode上文件/目录元数据在特定某一时刻的持久化存储文件.它的作用不言而喻,在HA出现之前,NameNode因为各种原因宕机后,若要恢复或在其他机器上重启 ...
- linux下tomcat6无法显示图片验证码 少了图形插件
linux下tomcat6无法显示图片验证码(windows下显示正常) 原创 2015年10月20日 10:31:47 3526 linux下tomcat6无法显示图片验证码(windows下显示正 ...
- MySql 数据库系列问题
0. 我的MYSQL学习心得(四) 数据类型(系列文章) 1.MySql数据库学习--存储过程(1) 0.[转]MySQL存储过程调试工具-dbForge Studio for MySQL ①.存储过 ...
- 常见UI组件的一个模板
效果: 代码: using UnityEngine; using UnityEditor; using System.Collections; using System.IO; using Unity ...
- every row of W is a classifier for one of the classes
every row of W is a classifier for one of the classes As we saw above, every row of W is a classifie ...
- js函数的caller属性
funcName.caller : 返回一个对函数的引用, 该函数调用了当前函数 function test() { if (test.caller) { var a = test.caller.to ...
- Python菜鸟之路:Python基础-Socket基础-1
预热知识 OSI 七层模型 谈到TCP/IP,就不得不说OSI七层模型,OSI 是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参考模型,为开放式互连信息系统提供 ...