LINQ查询
程序员文章站
2022-07-04 08:44:36
...
1.Person类
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; set; }
public string ProfessionName { get; set; }
public override string ToString()
{
return string.Format("{0} {1} {2} {3} {4}",Id, Name, Age, Department, ProfessionName);
}
}
2.Profession类
public class Profession
{
public string Name { get; set; }
public int Level { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", Name, Level);
}
}
3.建立Person集合
var personList = new List<Person>()
{
new Person(){ Id=1,Name="April",Age =19,Department="财务部",ProfessionName = "会计" },
new Person(){ Id=2,Name="Harris",Age =22,Department="研发部",ProfessionName = "工程师" },
new Person(){ Id=3,Name="Tom",Age =34,Department="研发部",ProfessionName = "工程师" },
new Person(){ Id=4,Name="Dave",Age =36,Department="行政部",ProfessionName = "行政专员" }
};
4.建立Profession集合
var professionList = new List<Profession>()
{
new Profession(){ Name = "会计", Level =100 },
new Profession(){ Name = "工程师", Level =200 },
new Profession(){ Name = "行政专员", Level =50 },
new Profession(){ Name = "厨师", Level =30 }
};
5.查询
查询集合personList中研发部年龄大于30的人。
5.1表达式
var res = from p in personList where p.Age>30 && p.Department=="研发部" select p;
5.2扩展方法
var res2 = personList.Where(person => person.Age>30 && person.Department=="研发部");
5.3查询结果
Id | Name | Age | Department | ProfessionName |
3 | Tom | 34 | 研发部 | 工程师 |
6.联合查询
查询职业等级大于100的人员信息。
6.1表达式
var res3 = from person in personList
from profession in professionList
where person.ProfessionName == profession.Name && profession.Level >100
select person;
6.2扩展方法
var res4 = personList.SelectMany(person => professionList, (per, pro) => new { first = per, second = pro })
.Where(x => x.first.ProfessionName == x.second.Name && x.second.Level >100)
.Select(x => x.first);
6.3 Join on
var res5 = from person in personList
join profession in professionList
on person.ProfessionName equals profession.Name
where profession.Level > 100
select person;
6.4查询结果
Id | Name | Age | Department | ProfessionName |
2 | Harris | 22 | 研发部 | 工程师 |
3 | Tom | 34 | 研发部 | 工程师 |
7.对查询结果排序
将研发部的人员按年龄从大到小排序。
7.1表达式
var res6 = from p in personList where p.Department == "研发部"
orderby p.Age descending
select p;
7.2扩展方法
OrderBy根据升序对元素排序
OrderByDescending根据降序对元素排序
OrderBy ..ThenBy..第一个排序条件相同时,按照第二个排序条件排序
var res7 = personList.Where(p => p.Department=="研发部").OrderByDescending(p=>p.Age);
7.3排序结果
Id | Name | Age | Department | ProfessionName |
3 | Tom | 34 | 研发部 | 工程师 |
2 | Harris | 22 | 研发部 | 工程师 |
8.分组查询
8.1将人员按照部门分组
var res8 = from person in personList
group person by person.Department into p
select new { Department = p.Key, Count = p.Count() };
8.2分组结果
Department | Count |
财务部 | 1 |
研发部 | 2 |
行政部 | 1 |
8.3查询每种专业的人数
var res9 = from profession in professionList
join person in personList
on profession.Name equals person.ProfessionName
into groups
select new { ProfessionName = profession.Name, Count = groups.Count() };
8.4查询结果
ProfessionName | Count |
会计 | 1 |
工程师 | 2 |
行政专员 | 1 |
厨师 | 0 |
9.量词操作符
9.1 Any
查询是否有厨师。Any查询时,集合中元素一个满足条件即返回ture.
bool res10 = personList.Any(p=>p.ProfessionName=="厨师"); // false
9.2 All
查询员工是否都大于18岁。All查询时,集合中元素都满足条件才返回True。
bool res11 = personList.All(p => p.Age > 18); //true
10.代码实例
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace stringTest
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; set; }
public string ProfessionName { get; set; }
public override string ToString()
{
return string.Format("{0} {1} {2} {3} {4}",Id, Name, Age, Department, ProfessionName);
}
}
public class Profession
{
public string Name { get; set; }
public int Level { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", Name, Level);
}
}
class Program
{
static void Main(string[] args)
{
var personList = new List<Person>()
{
new Person(){ Id=1,Name="April",Age =19,Department="财务部",ProfessionName = "会计" },
new Person(){ Id=2,Name="Harris",Age =22,Department="研发部",ProfessionName = "工程师" },
new Person(){ Id=3,Name="Tom",Age =34,Department="研发部",ProfessionName = "工程师" },
new Person(){ Id=4,Name="Dave",Age =36,Department="行政部",ProfessionName = "行政专员" }
};
var professionList = new List<Profession>()
{
new Profession(){ Name = "会计", Level =100 },
new Profession(){ Name = "工程师", Level =200 },
new Profession(){ Name = "行政专员", Level =50 },
new Profession(){ Name = "厨师", Level =30 }
};
//表达式
//查询集合personList中研发部年龄大于30的人
var res = from p in personList where p.Age>30 && p.Department=="研发部" select p;
//扩展方法写法
var res2 = personList.Where(person => person.Age>30 && person.Department=="研发部");
//联合查询
//查询职业等级大于100的人
//表达式
var res3 = from person in personList
from profession in professionList
where person.ProfessionName == profession.Name && profession.Level >100
select person;
//扩展方法
var res4 = personList.SelectMany(person => professionList, (per, pro) => new { first = per, second = pro })
.Where(x => x.first.ProfessionName == x.second.Name && x.second.Level >100)
.Select(x => x.first);
//Join on
var res5 = from person in personList
join profession in professionList
on person.ProfessionName equals profession.Name
where profession.Level > 100
select person;
//对查询结果排序
//研发部的人员按年龄从大到小排序
//表达式
var res6 = from p in personList where p.Department == "研发部"
orderby p.Age descending
select p;
var res7 = personList.Where(p => p.Department=="研发部").OrderByDescending(p=>p.Age);
//分组查询
//按部门分组
var res8 = from person in personList
group person by person.Department into p
select new { Department = p.Key, Count = p.Count() };
//查询每种专业的人数
var res9 = from profession in professionList
join person in personList
on profession.Name equals person.ProfessionName
into groups
select new { ProfessionName = profession.Name, Count = groups.Count() };
//量词操作符
//查询是否有厨师
bool res10 = personList.Any(p=>p.ProfessionName=="厨师");
//查询员工是否都大于18岁
bool res11 = personList.All(p => p.Age > 18);
Console.ReadLine();
}
}
}