LINQ基础篇(中)

聚合函数

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 listStudent
 		join c in listClasInfo
 		on s.ClassNo equals c.ClassNo
 		select 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 listStudent
 		    join c in listClassInfo
 		    on s.ClassNo equals c.ClassNo into scLink
 		    from 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 listStudent
		    join c in listClassInfo
		    on s.ClassNo equals c.ClassNo into scLink
		    from s in scLink.DefaultIfEmpty()
		    let sc="中华一番"+c?.ClassName
		    select new 
		    {
			   studentname = s.StudentName,
			   classname = sc
		    };
	foreach (var item in linp)
	{
		Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname);
	}
	Console.ReadKey();

上述代码在班级前加“中华一番”
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值