linq:.net,3.5出的一种技术,可以对任何一种数据集合进行筛选
1. from指定数据源和范围变量
select指定当执行查询时,返回序列中的元素将具有的类型和形式
group按照指定的键值对查询的结果进行分组
where根据一个或多个逻辑"与","或"运算符分割的布尔表达式筛选源元素
orderby基于元素类型的默认比较器按升序或降序对查询结果进行排序
join基于两个指定匹配条件之间的相等比较来联接两个数据源
let引入一个用于查询表达式中的子表达式结果的范围变量
into,提供一个标示符,它可以充当对join子句,group子句,或select子句的结果引用
eg:
int[] aNum = { 23, 12, 11, 9, 5, 7, 9, 22, 33 }; var aQuery = from a in aNum orderby a select a; foreach (var a in aQuery) { Console.WriteLine(a); }
倒序排列:
int[] aNum = { 23, 12, 11, 9, 5, 7, 9, 22, 33 }; var aQuery = from a in aNum orderby a descending select a; foreach (var a in aQuery) { Console.WriteLine(a); }
还可以用另外一种方式写,这种方式叫lambda表达式
int[] aNum = { 23, 12, 11, 9, 5, 7, 9, 22, 33 }; var aQuery = aNum.OrderBy(u => u); foreach (var a in aQuery) { Console.WriteLine(a); }
2.联合查询
class UserInfor { public int userId { get; set;} public string userName { get; set;} public string userPwd { get; set;} public string className { get; set; } }
class stuAsk { public int askId { get; set; } public int userId { get; set; } public string title { get; set; } public string text { get; set; } }
class Program { static void Main(string[] args) { List<UserInfor> userInfor = new List<UserInfor>() { new UserInfor() { userId=1,userName="sa",userPwd="sa1234",className="一班"}, new UserInfor() { userId=2,userName="sb",userPwd="sb1234",className="二班"}, new UserInfor() { userId=3,userName="sc",userPwd="sc1234",className="三班"}, new UserInfor() { userId=4,userName="sd",userPwd="sd1234",className="四班"}, new UserInfor() { userId=5,userName="se",userPwd="se1234",className="一班"} }; List<stuAsk> stuask = new List<stuAsk>() { new stuAsk(){askId=1,userId=1,title="一班",text="关于我们一班"}, new stuAsk(){askId=2,userId=2,title="二班",text="关于我们二班"}, new stuAsk(){askId=3,userId=3,title="三班",text="关于我们三班班"}, new stuAsk(){askId=4,userId=4,title="四班",text="关于我们四班"} }; var aQuery = (from sd in userInfor join sa in stuask on sd.userId equals sa.userId where sd.className == "一班" select new { sd, sa }).ToList(); Console.ReadKey(); }
3.let
var groupA = new[] { 3, 4, 5, 8, 10, 30 }; var groupB = new[] { 3, 6, 7, 8, 9, 2, 3 }; var someInts = from a in groupA from b in groupB let sum = a + b where sum == 12 select new { a, b, sum }; foreach (var a in someInts) { Console.WriteLine(a); }
4.分组group查询
List<UserInfor> list = new List<UserInfor>() { new UserInfor(){userId=1,userName="sa",className="一班"}, new UserInfor() { userId=2,userName="sb",className="二班"}, new UserInfor() { userId=3,userName="sc",className="三班"}, new UserInfor() { userId=4,userName="sd",className="四班"}, new UserInfor() { userId=5,userName="se",className="一班"} }; //g.Count()分组的一个数,班级总数,通过班级数量排序,越大越在前面 var userQuery = from u in list group u by u.className into g orderby g.Count() descending, g.Key where g.Count() > 0 select g.Key + "" + g.Count(); foreach (var n in userQuery) { Console.WriteLine(n); }
5.分页 用的比较多
List<UserInfor> list = new List<UserInfor>() { new UserInfor(){userId=1,userName="sa",className="一班"}, new UserInfor() { userId=2,userName="sb",className="二班"}, new UserInfor() { userId=3,userName="sc",className="三班"}, new UserInfor() { userId=4,userName="sd",className="四班"}, new UserInfor() { userId=5,userName="se",className="一班"} }; var aQuery = (from u in list select u).Skip(1).Take(3); //Skip,行号,跳过第几行,Skip(1),相当于Skip>1,Take(3),从第二行开始,取前三条数据 //pageIndex当前页,pagesize一页多少条数据 //skip((pageIndex-1)*pagesize).Take(pagesize) //假如pageIndex=1,pagesize=10 //skip((1-1)*10).Take(10),从第一条开始取前10条数据 //skip((2-1)*10).Take(10),skip>10,从第11条开始,取前10条数据 foreach (var n in aQuery) { Console.WriteLine(n.userName); }