Linq标准查询操作符
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1_Linq标准查询
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("曹操", 50);
Person P2 = new Person("诸葛亮", 20);
Person P3 = new Person("刘备", 40);
Person p4 = new Person("蔡文姬", 16);
List<Person> list = new List<Person> { p1, P2, P3, p4 };
List<string> listName = list.Where(p => p.Age > 25) //过滤掉年龄不超过25岁的
.OrderByDescending(p => p.Age)//按照年龄降序排列
.Select(p => p.Name) //将查询结果投射成名字
.ToList(); //将结果转成集合
Console.WriteLine($"年龄超过25岁的共有{listName.Count}人,按年龄由大到小排的结果是:");
foreach (var item in listName)
{
Console.WriteLine(item);
}
Console.WriteLine("-------------------------------");
Console.WriteLine($"所有人的平均年龄是{list.Average(p => p.Age)}");
Console.WriteLine($"所有人的总年龄是{list.Sum(p => p.Age)}");
Console.WriteLine($"所有人的最大年龄是{list.Max(p => p.Age)}");
Console.WriteLine($"所有人的最小年龄是{list.Min(p => p.Age)}");
Console.WriteLine("-------------------------------------------------------");
//获取唯一一个年龄大于25的人,如果有多个则抛出异常
Person p25 = list.Single(p => p.Age > 45);
Console.WriteLine($"唯一一个年龄大于45岁的是{p25.Age}岁的{p25.Name}");
Console.ReadKey();
}
}
public class Person
{
public Person(string name, int age)
{
this.Age = age;
this.Name = name;
}
public int Age { get; set; }
public string Name { get; set; }
}
}
linq表达式查询
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1_linq表达式查询
{
class Program
{
static void Main(string[] args)
{
//创建一个数据源
int[] numbers = new int[] { 0, 1, 2, 3, 4, 5 };
//创建查询
IEnumerable<int> numQuery =
from num in numbers
where (num % 2) == 0
select num;
//执行查询
foreach (var item in numQuery)
{
Console.WriteLine(item);
}
Console.WriteLine("----------------------------");
List<int> list = new List<int> { 100, 5, 50, 1, 8, 21, 12 };
var _list = from i in list
where i < 10
orderby i
select i;
foreach (var item in _list)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Linq扩展方法查询
/*
* public static IEnumerable Where
* (this IEnumerable source, Func<TSource, bool> predicate);
*
* Func<TSource, bool>:委托、
*
* 扩展方法:
* 静态类里写一个静态的方法,参数列表最前面加个this+要扩展到的类型
* 使用场景:在不修改源码的基础上,为其他类型修改方法
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1_Linq扩展方法查询
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int> { 10, 9, 5, 20, 24, 6 };
IEnumerable<int> _list = list.Where(i => i < 10).OrderBy(i => i);
foreach (var item in _list)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
扩展方法
/*
* 扩展方法:
* 静态类里写一个静态的方法,参数列表最前面加个this+要扩展到的类型
* 使用场景:在不修改源码的基础上,为其他类型修改方法
*/
using System;
namespace 扩展方法
{
class Program
{
static void Main(string[] args)
{
Cal cal = new Cal();
int sum = cal.AddNew(1, 2, 3);
Console.WriteLine(sum);
Console.ReadKey();
}
}
public class Cal
{
public int Add(int a, int b)
{
return a + b;
}
//public int Add(int a,int b,int c)
//{
// return a + b + c;
//}
}
public static class CalExtend
{
public static int AddNew(this Cal cal, int a, int b, int c)
{
return a + b + c;
}
}
}