首页 > 代码库 > C#webform LinQ

C#webform LinQ

LinQ的高级查询:

模糊查

con.Car.Where(r=>r.Name.Contains(cname)).ToList();


以..开头

con.car.Where(r => r.name.StartsWith(TextBox1.Text)).ToList();

 

以..结尾

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 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();

 

C#webform LinQ