var rest = from mast in master where mast.level > 6 select mast; //from后面设置查询的集合、where 后面跟查询条件、表示mast的结果集合返回
多条件:where mast.level > 6 && mast.menpai== "A机构"
排序:orderby 倒序排序后面加 descending
基础数据:
var master = new List<ArtsMaster>()
{
new ArtsMaster(){id=1,name="张三",age=16,menpai="A机构",kongfu="数据库工程师",level=5},
new ArtsMaster(){id=2,name="李四",age=36,menpai="B机构",kongfu="前端开发工程师",level=7},
new ArtsMaster(){id=3,name="王五",age=28,menpai="C机构",kongfu="后端开发工程师",level=6},
new ArtsMaster(){id=4,name="赵六",age=21,menpai="D机构",kongfu="大数据工程师",level=5},
new ArtsMaster(){id=5,name="钱七",age=19,menpai="A机构",kongfu="Android开发工程师",level=8},
new ArtsMaster(){id=6,name="孙八",age=19,menpai="B机构",kongfu="前端开发工程师",level=4},
new ArtsMaster(){id=7,name="李九",age=19,menpai="C机构",kongfu="后端开发工程师",level=5},
new ArtsMaster(){id=8,name="周十",age=19,menpai="A机构",kongfu="Android开发工程师",level=9},
new ArtsMaster(){id=9,name="十一",age=19,menpai="C机构",kongfu="数据库工程师",level=8},
};
var kongfu = new List<Kongfu>()
{
new Kongfu(){id=1,name="数据库工程师",power=100},
new Kongfu(){id=2,name="前端开发工程师",power=90},
new Kongfu(){id=3,name="后端开发工程师",power=80},
new Kongfu(){id=4,name="大数据工程师",power=95},
new Kongfu(){id=5,name="Android开发工程师",power=85}
};
1. 使用LINQ做查询,表达式写法
查询level>6的人
//查询level>6的所有人
//使用foreach查询
var list = new List<ArtsMaster>();
foreach(var temp in master)
{
if (temp.level > 6)
{
list.Add(temp);
}
}
//使用linq查询
var rest = from mast in master //from后面设置查询的集合
where mast.level > 6 //where 后面跟查询条件
select mast; //表示mast的结果集合返回
//输出list
foreach(var temp in rest)
{
Console.WriteLine(temp.ToString());
}
id:2,name:李四,age:36,menpai:B机构,kongfu:前端开发工程师,level:7
id:5,name:钱七,age:19,menpai:A机构,kongfu:Android开发工程师,level:8
id:5,name:钱七,age:19,menpai:A机构,kongfu:Android开发工程师,level:9
id:5,name:钱七,age:19,menpai:C机构,kongfu:数据库工程师,level:8
2. 扩展的写法
{
var re = master.Where(Test); //委托类型
var res = master.Where(mas => mas.level > 6); //lambda表达式
var res1 = master.Where(mas => mas.level > 6 && mas.menpai=="A机构"); //多条件
//输出list
foreach(var temp in res)
{
Console.WriteLine(temp.ToString());
}
}
//过滤方法
public static bool Test(ArtsMaster master)
{
if (master.level > 6)
return true;
else
return false;
}
3. LINQ联合查询
//联合查询
//输出所有的信息,一个mast映射5个kongfu
var restu = from mast in master
from kong in kongfu
select new { master = mast,kongfu = kong}; //两个都取到就new一个新的对象
//按条件查询,一个mast只对应一个kong
var restu = from mast in master
from kong in kongfu
where mast.kongfu == kong.name
select mast;
//输出kong中能力>9的所有人
var restu = from mast in master
from kong in kongfu
where mast.kongfu == kong.name && kong.power>90
select mast;
//使用委托
var restu = master.SelectMany(m => kongfu, (m, k) => new { master = m, kongfu = k })
.Where(x=>x.master.kongfu == x.kongfu.name && x.kongfu.power>90);
//master.SelectMany(委托) m => kongfu表示与master联合的表 (m, k) => new { master = m, kongfu = k }表示查询的结果委托成一个对象
//.Where(条件) x表示查询到的集合:(m, k) => new { master = m, kongfu = k }
4. 排序
var r = from mast in master
where mast.level > 7
orderby mast.level,mast.age //按照多个字段进行排序,如果字段的属性相同,就按照第二个字段的属性排序
select mast;
或者
var r = master.Where(m => m.level > 7).OrderBy(m => m.level).ThenByDescending(m => m.age);
id:9,name:十一,age:43,menpai:C机构,kongfu:数据库工程师,level:8
id:5,name:钱七,age:19,menpai:A机构,kongfu:Android开发工程师,level:8
id:8,name:周十,age:35,menpai:A机构,kongfu:Android开发工程师,level:9
5. join on 集合联合
//join on集合联合
var resu = from m in master
join k in kongfu on m.kongfu equals k.name
where k.power > 90
select new { master = m, kongfu = k };
//join后面跟联合的表,on后面跟连接条件,equals表示相等,where跟查询条件
{ master = id:1,name:张三,age:16,menpai:A机构,kongfu:数据库工程师,level:5, kongfu = id:1,name:数据库工程师,power:100 }
{ master = id:4,name:赵六,age:21,menpai:D机构,kongfu:大数据工程师,level:5, kongfu = id:4,name:大数据工程师,power:95 }
{ master = id:9,name:十一,age:43,menpai:C机构,kongfu:数据库工程师,level:8, kongfu = id:1,name:数据库工程师,power:100 }
6. 分组查询
//分组查询,哪个kongfu学习的人最多
var resul = from k in kongfu
join m in master on k.name equals m.kongfu
into groups //按照kongfu进行分组
orderby groups.Count()
//select new { kongfu = k, master = m }; //分类后是学习的人是一组一组的,不是单个m
select new { kongfu = k,conunt=groups.Count() };//conunt=groups.Count()获得这个组有多少人
{ kongfu = id:4,name:大数据工程师,power:95, conunt = 1 }
{ kongfu = id:1,name:数据库工程师,power:100, conunt = 2 }
{ kongfu = id:2,name:前端开发工程师,power:90, conunt = 2 }
{ kongfu = id:3,name:后端开发工程师,power:80, conunt = 2 }
{ kongfu = id:5,name:Android开发工程师,power:85, conunt = 2 }
//按照字段进行分组
var result = from m in master
group m by m.menpai into g
select new { count = g.Count(), key =g.Key };
//group 跟着表 by通过什么分组 into 分组的名字 g.Count()分组的人数,g.Key按照哪个字段进行分组
{ count = 3, key = A机构 }
{ count = 2, key = B机构 }
{ count = 3, key = C机构 }
{ count = 1, key = D机构 }
7. any all 是否满足条件
//量词操作符 any all 判断集合中是否满足某个条件
bool b = master.Any(m => m.menpai == "E机构");//是否有任意一个满足
Console.WriteLine(b); //false
bool x = master.All(m => m.menpai == "A机构");//是否全部满足
Console.WriteLine(x);//false
8. Skip:跳过指定个元素查询
var ss = (from r in db.Am_recProScheme
orderby r.rpId descending
select r).Skip(10); // 跳过前10条数据,取10条之后的所有数据
9. Take:只查询指定个元素、分页数据查询
var ss = (from r in db.Am_recProScheme
where r.rpId > 10
orderby r.rpId descending
select r).Skip(10).Take(10); // 取第11条到第20条数据
// 2 Take(10): 数据从开始获取,获取指定数量(10)的连续数据
var ss1 = db.Am_recProScheme.OrderByDescending(p= > p.rpId).Where(p= > p.rpId > 10).Skip(10).Take(10).ToList();
标签:name,kongfu,level,LINQ,学习,master,new,id From: https://www.cnblogs.com/Joyce-mi7/p/16965689.html