查询
1.查询所有的。
var query = from p in _Context.Info select p;
var query = _Context.Info;
2.单条件查询
等值查
var query = from p in _Context.Info where p.Code == "p002" select p;
注意:双等号,双引号。---C#语法。
var query = _Context.Info.Where(p => p.Code == "p002" ); //Lambda表达式
不等值查
//var query = from p in _Context.Info where p.Code != "p002" select p;
//var query = _Context.Info.Where(p => p.Code != "p002"); //Lambda表达式
//var query = from p in _Context.Info where p.Birthday.Value.Year > 1985 select p;
var query = _Context.Info.Where(p=>p.Birthday.Value.Year < 1985);
3.多条件查询
//var query = from p in _Context.Info where p.Sex == true && p.Birthday.Value.Year > 1985 select p;
//var query = _Context.Info.Where(p=>p.Sex==true && p.Birthday.Value.Year>1985);
var query = _Context.Info.Where(p => p.Sex == true).Where(p=>p.Birthday.Value.Year>1985) ; //多条件逻辑与可以用链式表达式写
4.模糊查询
Contains 确认查询字是否包含
StartsWith 确认查询字开头是否与字符串匹配
EndsWith 确认查询字结尾是否与字符串匹配
Substring 查询字符串从指定字符开始具有指定的长度
//var query = from p in _Context.Info where p.Name.Contains("张") select p; //相当于like '%张%'
//var query = from p in _Context.Info where p.Name.StartsWith("张") select p; // like '张%'
//var query = from p in _Context.Info where p.Name.EndsWith("张") select p; //like '%张'
//var query = from p in _Context.Info where p.Name.Substring(1,1) == "张" select p;// like '_张%'
var query = _Context.Info.Where(p => p.Name.StartsWith("张"));
5.集合操作
var q1 = from p in _Context.Info where p.Sex== true select p;
var q2 = _Context.Info.Where(p => p.Birthday.Value.Year > 1985);
交
Intersect 对值进行比较生成2个序列的交集
var query = q1.Intersect(q2);
并
Union 生成2个序列的并集
var query = q1.Union(q2);
差
Except 对值进行比较产生2个序列的差集
var query = q2.Except(q1);
6.统计函数:
var query = _Context.Info.Where(p => p.Birthday.Value.Year > 1985);
//this.Text = query.Count().ToString();
//this.Text = query.Sum(p=>DateTime.Now.Year - p.Birthday.Value.Year).ToString();
//this.Text = query.Average(p => DateTime.Now.Year - p.Birthday.Value.Year).ToString();
//this.Text = query.Max(p => DateTime.Now.Year - p.Birthday.Value.Year).ToString();
this.Text = query.Min(p => DateTime.Now.Year - p.Birthday.Value.Year).ToString();
7.转换操作
Tolist() 创建一个集合
ToArray() 创建一个数组
First() 创建一个对象 first第一个
ToList(),ToArray(),First()/Single()
8.分页
kip(2*2) 跳过2条 分成2也 Take(2)显示2条
var query = _Context.Info.Skip(2*2).Take(2);
|