一般查詢
db.User.Select(u => u); // 不帶條件查詢
db.User.Where(u => true); //不帶條件查詢
db.User.Where(u => u.username == "wjl" || u.username == "hyf"); // 帶條件查詢 || 表示 “或” && 表示 “且”
db.User.Select(u => u.username.EndsWith("麗")); // 模糊查詢 相當于like '%麗'
db.User.Select(u => u.username.IndexOf("麗")); // 模糊查詢 相當于like '%麗%'
db.User.Select(u => u.username.StartsWith("麗")); // 模糊查詢 相當于like '麗%'
db.User.Where( u => (u.username == user.username && u.userpwd == user.userpwd)).Count(); // 計數 回傳int型別的數值
聚合函式查詢
//最大值 var list = from p in db.Products group p by p.CategoryID into g select new { g.Key, MaxPrice = g.Max(p => p.UnitPrice) }; //最小值 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, MaxPrice = g.Max(p => p.UnitPrice) }; //平均值 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, AveragePrice = g.Average(p => p.UnitPrice) }; //求和 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, TotalPrice = g.Sum(p => p.UnitPrice) }; //計數 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, NumProducts = g.Count() }; //帶條件計數 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, NumProducts = g.Count(p => p.Discontinued) };
高級查詢
//in查詢 var list1 = db.Users.Where(u => new int[] { 1, 2, 3 }.Contains(u.Id)); var list2 = from u in db.Users where new int[] { 1, 2, 3 }.Contains(u.Id) select u; //分頁查詢,按需查詢所要的欄位 var list3 = db.Users.Where(u => new int[] { 1, 2, 3 }.Contains(u.Id)) .OrderBy(u => u.Id) .Select(u => new { Account = u.Account, Password = u.Password }).Skip(3).Take(5); var list4 = (from u in db.Users where new int[] { 1, 2, 3 }.Contains(u.Id) orderby u.Id select new { Account = u.Account, Pwd = u.Password }).Skip(3).Take(5); //多條件查詢的另一種寫法 var list5 = db.Users.Where(u => u.Name.StartsWith("小") && u.Name.EndsWith("新")) .Where(u => u.Name.EndsWith("新")) .Where(u => u.Name.Contains("小新")) .Where(u => u.Name.Length < 5) .OrderBy(u => u.Id); //連接查詢,inner join var list6 = from u in db.Users join c in db.Companies on u.CompanyId equals c.Id where new int[] { 1, 2, 3, 4, 6, 7, 10 }.Contains(u.Id) select new { Account = u.Account, Pwd = u.Password, CompanyName = c.Name }; //連接查詢,left join var list7 = from u in db.Users join c in db.Categories on u.CompanyId equals c.Id into ucList from uc in ucList.DefaultIfEmpty() where new int[] { 1, 2, 3, 4, 6, 7, 10 }.Contains(u.Id) select new { Account = u.Account, Pwd = u.Password };
分頁查詢,引數的動態改變自己去設定OrderBy為升序, OrderByDescending為降序 ,ThenByDescending與ThenBy為第二條件排序,Skip相當于not in ,Take相當于Top
var userlist = db.User.Where<User>(u => true).OrderByDescending(u => u.userid).ThenBy(u => u.username).Skip((pageindex - 1) * pagesize).Take(pagesize); int pageindex; //從第幾條開始 if (!int.TryParse(Request["pageindex"], out pageindex)) { pageindex = 1; } int rcordcount = db.User.Count(); //統計總記錄數 int pagesize = 5; //每頁要顯示的記錄條數 int pagecount = Convert.ToInt32(Math.Ceiling((double)rcordcount / pagesize)); //計算頁數 pageindex = pageindex < 1 ? 1 : pageindex; //pageindex不能小于1 和 pageindex 不能大于記錄總數 pageindex = pageindex > pagecount ? pagecount : pageindex; // OrderBy為升序, OrderByDescending為降序 ,ThenByDescending與ThenBy為第二條件排序,Skip相當于not in ,Take相當于Top var userlist = db.User.Where<User>(u => true).OrderByDescending(u => u.userid).ThenBy(u => u.username).Skip((pageindex - 1)* pagesize).Take(pagesize);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/288711.html
標籤:其他
