查询所有
var q = from p in context.Info select p;
var q = context.Info;
但条件查询
var q = from p in context.Info where p.Code=="p003" select p;
var q = context.Info.Where(p => p.Code=="p003");
多条件查询
var q = from p in context.Car where p.Price > 30 && p.Brand=="b002" select p;
var q = context.Car.Where(p => p.Price > 30 && p.Brand == "b002");
var q = context.Car.Where(p => p.Price > 30).Where(p => p.Brand == "b002");
var q = from p in context.Car where p.Price > 30 || p.Brand == "b002" select p;
var q = context.Car.Where(p => p.Price > 30 || p.Brand == "b002");
模糊查询:
var q = from p in context.Car where p.Name.Contains("5") select p; //包含
var q = from p in context.Car where p.Name.StartsWith("奥迪") select p;//开头
var q = from p in context.Car where p.Name.EndsWith("奥迪") select p;//结尾
var q = context.Car.Where(p => p.Name.Contains("5"));//包含
var q = context.Car.Where(p => p.Name.StartsWith("奥迪"));//开头
var q = context.Car.Where(p => p.Name.EndsWith("型"));//结尾
var q = from p in context.Car where p.Name.Substring(2, 1) == "5" select p;//第三个字符
var q = context.Car.Where(p => p.Name.Substring(2,1) == "5");//第三个字符是5
Distinct查询
var q = (from p in context.Car select p.Brand).Distinct();//去重
var q = context.Car.Select(p=>p.Brand).Distinct();//去重
连接查询——对象之间的关联关系点出来的。
var q = context.Car.Where(p => p.Brand1.Productor.Prod_Name=="一汽丰田");