linq基础
电脑版发表于:2021/3/11 10:03
前言
linq使用基础
一、 什么是Linq:
语言集成查询
二、 linq作用:
简化查询
三、 Linq分类:
Linq to object :linq操作内存集合
Linq to xml : linq 操作xml
Linq to sql : linq操作数据库
四、 Linq语法:
Var 返回值 = from 变量名 in 数据源(可以是数据库,也可以是集合)
Where 变量名.xx = “”
Order by 排序
Join 连接查询
Select 返回值
//例子(根据年龄查询) List<Users> list = new List<Users>(){ new Users() { UserName = "刘备", Age = 54, Address = "蜀" }, new Users() { UserName = "刘邦", Age = 53, Address = "楚" }, new Users() { UserName = "关羽", Age = 52, Address = "蜀" }, new Users() { UserName = "曹操", Age = 54, Address = "魏" }, new Users() { UserName = "张飞", Age = 50, Address = "蜀" }, new Users() { UserName = "马超", Age = 44, Address = "西凉" }, new Users() { UserName = "李白", Age = 33, Address = "唐" } }; //linq 标准写法 var str = from name in list where name.Age > 50 orderby name.Age descending select name; foreach (var item in str) { Console.WriteLine( String.Format("姓名:{0} 年龄:{1} 地址:{2}",item.UserName, item.Age, item.Address)); }