using System.Linq;
IEnumerable
static void F1()
{
int[] dt = new int[] { 1,2,3,4,5,6,7,8,9,10};
IEnumerable<int> items = dt.Where(i => i > 5);
foreach (var item in items)
{
Console.WriteLine(item);
}
}
可以将数组 转化为 foreach 数组使用 。
Where()
使用方法
List<Employee> list = new List<Employee>();
list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });
IEnumerable<Employee> Items1 = list.Where(e => e.Age > 30);
foreach (var item in Items1)
{
Console.WriteLine(item.Name);
}
Count()
static void Main(string[] args)
{
List<Employee> list = new List<Employee>();
list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });
IEnumerable<Employee> Items1 = list.Where(e => e.Age > 30);
foreach (var item in Items1)
{
Console.WriteLine(item.Name);
}
Console.WriteLine(list.Count());
Console.WriteLine(list.Count(a=>a.Age>30));
}
且: &&
any 是碰到一条就返回,性能比 Count 性能更好
Single , SingleOrDefault
//single //有多条数据就会报错,没有数据的时候也会报错
Employee e1 = list.Where(e=>e.Name=="jerry").Single();
Console.WriteLine(e1.Name);
Employee e3 = list.SingleOrDefault(i=>i.Name=="123");
Console.WriteLine(e3 == null);
Employee e2 = list.First();//有多条数据 返回第一条
Console.WriteLine(e2);
Employee e4 = list.FirstOrDefault(i => i.Name == "123");//有多条数据 返回第一条
Console.WriteLine(e4==null);
using System;
using System.Collections.Generic;
using System.Linq;
namespace lambda表达式应用
{
class Program
{
static void Main(string[] args)
{
List<Employee> list = new List<Employee>();
list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });
IEnumerable<Employee> Items1 = list.Where(e => e.Age > 30);
foreach (var item in Items1)
{
// Console.WriteLine(item.Name);
}
// Console.WriteLine(list.Count());
// Console.WriteLine(list.Count(a=>a.Age>30));
//single //有多条数据就会报错,没有数据的时候也会报错
Employee e1 = list.Where(e=>e.Name=="jerry").Single();
Console.WriteLine(e1.Name);
Employee e3 = list.SingleOrDefault(i=>i.Name=="123");
Console.WriteLine(e3 == null);
Employee e2 = list.First();//有多条数据 返回第一条
Console.WriteLine(e2);
Employee e4 = list.FirstOrDefault(i => i.Name == "123");//有多条数据 返回第一条
Console.WriteLine(e4==null);
}
class Employee
{
public long Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool Gender { get; set; }
public int Salary { get; set; }
}
}
}
Order(), OrderByDescending() 排序
//升序
IEnumerable<Employee> item2 = list.OrderBy(e => e.Age);
foreach (Employee item in item2)
{
Console.WriteLine(item.Age);
}
//降序
IEnumerable<Employee> item3 = list.OrderByDescending(e => e.Age);
foreach (Employee item in item3)
{
Console.WriteLine(item.Age);
}
限制结果集
//调过三条数据 取两条数据
var items = list.Skip(3).Take(2);
foreach (var e in items)
{
Console.WriteLine(e);
}
稍微复杂点:
var items3 = list.Where(e => e.Age > 30)
.OrderBy(e => e.Age).Skip(1).Take(2);
foreach (var e in items3)
{
Console.WriteLine(e.Name);
}
聚合函数 : MAX MIN Average sum count
GroupBy() 分组
var dt2= list.GroupBy(e => e.Age);
foreach (var item in dt2)
{
Console.WriteLine(item.Key);
foreach (var items in item)
{
Console.WriteLine(items.Name);
}
}
投影操作 select()
Select()
映射:查出来某一个字段或者是字符串
//投影 select()
//只取出一个字段的数值
IEnumerable<int> items4= list.Select(e => e.Age);
foreach (var item in items4)
{
Console.WriteLine(item);
}
IEnumerable<string> items4 = list.Where(e=>e.Age>20).Select(e =>"姓名:"+ e.Name+",年龄:"+e.Age);
foreach (var item in items4)
{
Console.WriteLine(item);
}
匿名类型投影:
//匿名类型
var obj1 = new {name="dd",AAA="dts",bbb="12345" };
遍历方便 ** 重点写法**
var dt1s= list.Select(e =>new
{ xiaomi = e.Name, dg = e.Age,dts=e.Salary });
foreach (var item in dt1s)
{
Console.WriteLine(item);
}
集合类型转换
ToArray()
ToList()
IEnumerable<Employee> dtlsit= list.Where(e => e.Salary > 6000);
List<Employee> dtlist2= dtlsit.ToList();
链式编程
var dtss1= list.Where(e => e.Id > 2).GroupBy(e => e.Age)
.OrderBy(g => g.Key).Take(3)
.Select(g => new { Nl = g.Key, RS = g.Count(), PJ = g.Average(e => e.Salary) });
另外的语法形式—查询语法
查询语法 —> 同上效果
Linq 解决面试中的问题
案例
string s = "1,232,213,45,12,512,543,234";
string[] strs= s.Split(',');
IEnumerable<int> intstr= strs.Select(e => Convert.ToInt32(e));
double avgnumber = intstr.Average();
Console.WriteLine(avgnumber);
double avgdt = s.Split(',')
.Select(e=>Convert.ToInt32(e))
.Average();
Console.WriteLine(avgdt);
string ss = "dasgdagfdbncewvrthjytbhdgeqcvreq";
var dss= ss.Where(c => char.IsLetter(c))
.Select(c => char.ToLower(c))
.GroupBy(c => c)
.Select(g => new { g.Key, count = g.Count() })
.OrderByDescending(g=>g.count)
.Where(g=>g.count>2);
foreach (var item in dss)
{
Console.WriteLine(item);
}