聚合函数

Max 最大值

Min 最小值

Sum 求和

Average 求平均值

Aggregate 自定义累计

Count 统计元素个数

LongCount 统计元素个数返回long型

代码例子

 List<int> tempList = new List<int> { 56,34,23,21,78,99 };var maxValue = tempList.Max();var minValue = tempList.Min();var sumValue = tempList.Sum();var avgValue = tempList.Average();var aggValue = tempList.Aggregate((initial,next) => initial+next * 2);var countValue = tempList.Count();var lcountValue = tempList.LongCount();Console.WriteLine("最大值:{0}\r\n最小值:{1}\r\n求和:{2}\r\n平均值:{3}\r\n自定义累计:{4}\r\n元素个数:{5}\r\n元素个数(long):{6}",maxValue,minValue,sumValue,avgValue,aggValue,countValue,lcountValue);Console.ReadKey();

结果

Aggregate

public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);

该函数有三种定义,其中Func是Lambada表达式,seed是初始增量

var aggValue = tempList.Aggregate(6,(initial, next) => initial+next * 2);

初始是6,表示2倍的和再加上6,即628,该函数灵活多变。

join连接

内连接 inner join

内连接和SQL是类似的

查询语法:var p=from a in A join b in B on a.No equals b.No select new {};

方法语法:var p=A.Join(B,a=>a.No,b=>b.No,(a,b)=>new {});

左连接 left outer join

查询语法:var p=from a in A join b in B on a.No equals b.No into abLink
from c in abLink DefaultIfEmpty()
select new {aa=a.No,bb=c==null?null:c.cc}

代码例子

定义Student类

public class Student
{public string StudentNo { get; set; }public string StudentName { get; set; }public string ClassNo { get; set; }
}

定义ClassInfo类

public class ClassInfo
{public string ClassNo { get; set; }public string ClassName { get; set; }
}

实现调用

 List<Student> listStudent = new List<Student>{new Student{StudentNo="1",StudentName="张三",ClassNo="1"},new Student{StudentNo="2",StudentName="李四",ClassNo="2"},new Student{StudentNo="3",StudentName="王五",ClassNo="3"},new Student{StudentNo="4",StudentName="刘三",ClassNo="1"},new Student{StudentNo="5",StudentName="关翊",ClassNo="2"},new Student{StudentNo="6",StudentName="张珊",ClassNo="3"},new Student{StudentNo="7",StudentName="赵四",ClassNo=""},};List<ClassInfo> listClassInfo = new List<ClassInfo>{new ClassInfo{ClassNo="1",ClassName="英才一班"},new ClassInfo{ClassNo="2",ClassName="英才二班"},new ClassInfo{ClassNo="3",ClassName="英才三班"},};var p = from s in listStudentjoin c in listClasInfoon s.ClassNo equals c.ClassNoselect new{studentname = s.StudentName,classname = c.ClassName,};var lp = listStudent.Join(listClassInfo,s=>s.ClassNo,c=>c.ClassNo,(s,c)=>new { studentname = s.StudentName,classname = c.ClassName}var linkp = from s in listStudentjoin c in listClassInfoon s.ClassNo equals c.ClassNo into scLinkfrom c in scLink.DeafaultIfEmpty()slect new {studentname = s.StudentName,classname = c?.ClassName//className = c==null?null:c.ClassName};foreach (var item in lp){Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname),}Console.ReadKey();            

输出结果

let
let用于设置临时变量储存结果,对数据进行操作

语法:let 变量a=操作。。。

 var linkp = from s in listStudentjoin c in listClassInfoon s.ClassNo equals c.ClassNo into scLinkfrom s in scLink.DefaultIfEmpty()let sc="中华一番"+c?.ClassNameselect new {studentname = s.StudentName,classname = sc};foreach (var item in linp){Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname);}Console.ReadKey();

上述代码在班级前加“中华一番”

LINQ基础篇(中)相关推荐

  1. 鸟哥的Linux私房菜(基础篇)中涉及的所有网址

    第零章.计算器概论 超级计算机(Supercomputer) 超级计算机是运行速度最快的电脑,但是他的维护.操作费用也最高!主要是用于需要有高速计算的计划中. 例如:国防军事.气象预测.太空科技,用在 ...

  2. IBM主机系列课程之单元测试(基础篇)-李海湜-专题视频课程

    IBM主机系列课程之单元测试(基础篇)-3681人已学习 课程介绍         IBM主机环境下进行的单元测试,课程包含单元测试重要知识点,从讲解到示例列举,带你逐步深入了解IBM主机的魅力所在. ...

  3. 【ShaderToy】基础篇之再谈抗锯齿(antialiasing,AA)

    写在前面 在之前的基础篇中,我们讲到了在绘制点线时如何处理边缘的锯齿,也就是使用smoothstep函数.而模糊参数是一些定值,或者是跟屏幕分辨率相关的数值,例如分辨率宽度的5%等等.但这种方法其实是 ...

  4. vue基础篇实战总结一:购物车实战

    上周看完了vue基础篇 本想继续看进阶篇 本着基础不牢地动山摇的原则 特地总结重新再写一遍基础篇中实战开发过的一些小东西,以加深熟练度 如果还没有开始vue学习的朋友可以看这本书,还不错 链接:htt ...

  5. SpringBoot2零基础到项目实战-基础篇

    springboot2零基础到项目实战-基础篇 课程内容说明 课程单元 学习目标 基础篇 能够创建SpringBoot工程 基于SpringBoot实现ssm/ssmp整合 应用篇 能够掌握Sprin ...

  6. 基础篇:9)装配流程图

    本章目的:作为结构工程师,学会制作装配流程图. 1.前言 装配流程图在本博文中的作用很大,所以提前在基础篇中讲解. 不要简单把装配流程图的工作丢锅给工艺工程师,因为装配的流程是由结构设计师/机械设计师 ...

  7. Linq初级班 Linq To XML体验(基础篇)

    LINQ To XML体验(基础) 这两天开始学习LINQ to XML的知识,我会继续把自己的感想和示例发布给初学者们学习的,一样欢迎高手们多多指点,请勿使用过激语言,针锋相对,我是个初学者,自知还 ...

  8. 数据库应用程序开发基础篇—— .NET中SQL Server数据库的操作C#篇之一

    数据库应用程序开发基础篇-- .NET中SQL Server数据库的操作C#篇之一 写在前面:前面介绍了数据库系统的基本概念,SQl语句基本使用方法,接下来通过学习具体语言和具体数据库结合的应用开发来 ...

  9. option:contains后面加变量_Python基础篇 -2:初识Python中的变量

    Python学习基础篇 -2 前言:本专栏以Python为主题,并尽可能保持每星期两到三更,直到将Python的基础知识浅析和讲解完毕,同时,有一定基础的同学可以移步 Python实战专栏 . 第二篇 ...

最新文章

  1. vb.net2019-多线程并行计算(5)
  2. kotlin学习笔记——枚举、封闭类
  3. 20个公司绝对不会告诉你的潜规则
  4. flex 下对齐_Flex 布局示例
  5. 重载跟重写--笔记2
  6. 基于tcpdump实例讲解TCP/IP协议
  7. @程序员,你真的懂数据可视化吗?
  8. 设计模式之GOF23备忘录模式
  9. python双层for循环实现全排列_Python循环实现n的全排列功能
  10. DFA敏感词过滤算法
  11. Git小乌龟合并代码
  12. 敏捷与CMMI的同与不同
  13. 微分方程_e的矩阵指数
  14. 【杂谈】真相揭开有点吃惊:杨利伟出舱时
  15. oracle lob类型和mysql text_OracleLob类型存储浅析
  16. 摸着石头过河__投石问路
  17. 手机查看python代码_30个极简Python代码,拿走即用
  18. 狗生活在陆地上 java,第四晚,生活在陆地上的鱼
  19. 科学幻想其实是对科学研究的发展方向起到很好的引领作用
  20. 四舍五入算法、五舍六入、六舍七入、七舍八入。。。。。。。

热门文章

  1. 计算机启动应用程序的方法,怎么启动Windows应用程序
  2. Oracle-基本SQL语句
  3. WPS for Linux使用测评
  4. 80端口、443端口、8080端口、8000端口的区别
  5. 【Computer Graphics】向量投影
  6. yaahp使用教程_如何用yaahp进行大量备选方案/评价对象的综合评价?
  7. 音频编码方案之间音质比较(AAC,MP3,WMA等)
  8. 图片文字转换为文本怎么做?图片转文本的简单方法介绍
  9. 7-1 计算物体自由下落的距离
  10. Windows编程 从消息窗口到基本窗口 游戏循环窗口框架的简单实现