LinQ高级查
在后台的类里写:
LinQ操作类名 con = new LinQ操作类名();
1、多条件查询:
并且 &&
var list = con.car.Where(r => r.code == "c014" && r.oil == 8.2m);
或者 ||
var list = con.car.Where(r => r.code == "c014" || r.oil == 8.2m);
2、模糊查询
1.模糊
var list = con.car.Where(r => r.name.Contains(""));
2.开头
var list = con.car.Where(r => r.name.StartsWith(""));
3.结尾
var list = con.car.Where(r => r.name.EndsWith(""));
3、统计查询
1.总个数
var count = con.car.Count();
2.最大值
var Max = con.car.Max(r => r.price);
3.最小值
var Min = con.car.Min(r => r.price);
4.平均值
var avg = con.car.Average(r => r.price);
4、分页查询
跳过,获取
int count = 5;
int pagecount = 1;
var list = con.car.Skip(pagecount * count).Take(count);
skip:跳过 take:取出或者显示
5、排序查询
升序
var list = con.car.OrderBy(r => r.price);
降序
var list = con.car.OrderByDescending(r => r.price);
去重:
var aaa = con.car.Select(r => r.oil).Distinct();
6、外键显示
public 类型 要扩展成的字段名{
get{
return this.brand1.brand_name;
}
}
7、组合查询
交集
var list = list1.Intersect(list2).Intersect(list3);
-------------------------------------------------------------
var list1 = con.car.AsQueryable();
var list2 = con.car.AsQueryable();
var list3 = con.car.AsQueryable();
if (TextBox1.Text.Trim().Length > 0)
{
list1 = list1.Where(r => r.name.Contains(TextBox1.Text.Trim()));
}
if (TextBox2.Text.Trim().Length > 0)
{
list2 = list2.Where(r => r.brand1.brand_name.Contains(TextBox2.Text.Trim()));
}
if (TextBox3.Text.Trim().Length > 0)
{
list3 = list3.Where(r => r.oil == Convert.ToDecimal(TextBox3.Text.Trim()));
}
var list = list1.Intersect(list2).Intersect(list3);
Repeater1.DataSource = list;
Repeater1.DataBind();