模糊查詢:
//數據庫 + 自定義名稱 =new 數據庫 //例子: mydbDataContext con = new mydbDataContext(); //模糊查詢表達式中用.Contains con.car.Where(r=>r.name.Contains(TextBox1.Text.Trim())).ToList(); //開頭查用.StartWith con.car.Where(r => r.name.StartsWith(TextBox1.Text)).ToList(); //結尾查用.EndWith con.car.Where(r => r.name.EndsWith(TextBox1.Text)).ToList();
最大值,最小值:
//最大值 //例子: con.car.Max(r => r.price * r.oil).ToString(); //最小值 //例子: con.car.Min(r => r.price).ToString();
求和,平均值:
//求和 con.car.Sum(r => r.price).ToString(); //平均值 con.car.Average(r => r.price).ToString();
升序,降序:
//升序: con.car.OrderBy(r => r.price).ToList(); //降序: con.car.OrderByDescending(r => r.price).ToList();
上一頁,下一頁,組合查詢:
int PageCount = 5;//每頁顯示條數 //上一頁,PageCount_Label.Text為當前頁碼 int pageNum = Convert.ToInt32(PageCount_Label.Text) - 1; Repeater1.DataSource = con.car.Skip((pageNum - 1) * PageCount).Take(PageCount); Repeater1.DataBind(); PageCount_Label.Text = pageNum.ToString(); //下一頁 int pageNum = Convert.ToInt32(PageCount_Label.Text) + 1; Repeater1.DataSource = con.car.Skip((pageNum - 1) * PageCount).Take(PageCount); Repeater1.DataBind(); PageCount_Label.Text = pageNum.ToString(); //組合查詢的點擊事件 List<car> list = con.car.ToList(); if (TextBox2.Text != "") { List<car> list1 = con.car.Where(r => r.name.Contains(TextBox2.Text)).ToList(); list = list.Intersect(list1).ToList(); } if (TextBox3.Text != "") { List<car> list1 = con.car.Where(r => r.oil == Convert.ToDecimal(TextBox3.Text)).ToList(); list = list.Intersect(list1).ToList(); } if (TextBox4.Text != "") { List<car> list1 = con.car.Where(r => r.powers == Convert.ToInt32(TextBox4.Text)).ToList(); list = list.Intersect(list1).ToList(); } Repeater1.DataSource = list; Repeater1.DataBind();