LinQ的高级查询:
-------------------
在car表格进行练习操作:
一.模糊查询:
1.在后台代码:(Contains)
List<car>list=con.car.where(r=>r.name.Contains
(TextBox1.Text.Trim())).ToList();
Repeater1.DataSource=list;
Repeater1.DAtaBind();
---------------------------
2.开头查:
Repeater1.DataSource=con.car.where
(r=>r.name.StartsWith(TextBox1.Text)).ToList();
Repeater1.DataBind();
---------------------
3.结尾查:
Repeater1.DataSource=con.car.where
(r=>r.name.EndsWith(TextBox1.Text)).ToList();
Repeater1.DataBind();
----------------------
--------------------------
数据操作查询:
1.最大值查询:(Min)
Label1.Text=con.car.Min(r=>r.price).Tostring();
2.最小值:(Max)
Label1.Text=con.car.Max(r=>r.price).Tostring();
3.求和(Sum)
Label1.Text=con.car.Sum(r=>r.price).Tostring();
4.平均值:(Average)
Label1.Text=con.car.Average(r=>r.price).Tostring
();
-----------------------------
--------------------------------
排序的查询:
1.降序:
Repeater1.DataSource=con.car.OrderByDescending
(r=>r.price).ToList();
Repeater1.DataBind();
2.升序:
Repeater1.DataSource=con.car.OrderBy
(r=>r.price).ToList();
Repeater1.DataBind();
--------------------------------
--------------------------------
分页显示:
PageCount_Label.Text = "1";
Repeater1.DataSource = con.car.Take(5);
Repeater1.DataBind();
//显示五页
组合查询:(需要去重,关键字:Intersect)
void Button10_Click(object sender, EventArgs e)
{
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();
}