标签:
linq:.net,3.5出的一种技术,可以对任何一种数据集合进行筛选
1.联合查询
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(); }
2.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); }
3.分组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); }
标签:
原文地址:http://www.cnblogs.com/wangwangwangMax/p/5507134.html