//id=@id var list=db.Queryable<Student>().Where(it => it.Id == id).ToList(); //多个条件 var list2=db.Queryable<Student>() .WhereIF(id>0,it => it.Id == id)// 如果id>0 添加条件 id=@id .WhereIF(name!=null,it => it.name== "a") //如果 name !=null 添加条件 name='a' .ToList(); //id=@id or name like '%'+@name+'%' var list2 = db.Queryable<Student>().Where(it => it.Id == id||it.Name.Contains("jack")).ToList();
表达式: && 表示 and , || 表式 OR
//id=@id var list=db.Queryable<Student>().Where("id=@id",new { id=1}).ToList(); //id=@id or name like '%'+@name+'%' var list2 = db.Queryable<Student>().Where("id=@id or name like '%'+@name+'%' ",new { id=1,name="jack"}).ToList();
[ {"FieldName":"id","ConditionalType":"0","FieldValue":"1"}, {"FieldName":"name","ConditionalType":"0","FieldValue":"jack"} ] //5.0.5.1 Json直接序列化 var conModels= db.Context.Utilities.JsonToConditionalModels(json) var student = db.Queryable<Student>().Where(conModels).ToList(); // id=1 and name=jack //手动构造 //var cs= new List<IConditionalModel>(); //cs.Add(new ConditionalModel{FieldName="id",ConditionalType=ConditionalType.Equal,FieldValue="1"}); //cs.Add(new ConditionalModel{FieldName="name",ConditionalType=ConditionalType.Equal,FieldValue="jack"})
点击查看: https://www.donet5.com/Home/Doc?typeId=2314
//创建表达式 var exp= Expressionable.Create<Student>() .And(it=>it.Id==1) .Or(it =>it.Name.Contains("jack")) .ToExpression();//注意 这一句 不能少 //使用表达式 var list=db.Queryable<Student>().Where(exp).ToList(); //多表查询 var exp=Expressionable.Create<T1,T2>()
var query = db.Queryable<Student>().Where(it=>it.Id==1); if(条件) query.Where(it => it.Name == "jack"); if(条件) query.Where(it => it.Id==1); int count=query.Clone().Count();//当query用于2个地方的时候一定要加Clone这点和EF有本质区别 var list=query.Clone.ToList();
var query = db.Queryable<Student>() .WhereIF(!string.IsNullOrEmpty(name), it => it.Name.Contains(name)) .WhereIF(id>0, it => it.Id==id).ToList();
var getAll = db.Queryable<Order>().WhereClass(new Order() { Name="a"},ignoreDefaultValue:true).ToList();//单个实体 var getAll = db.Queryable<Order>().WhereClass(List<Order>,ignoreDefaultValue:true).ToList();//支持集合 //ignoreDefaultValue=true 那么实体中的ID=0就不是条件,ID=1就会当作条件 //根据实体类查询
var getAll = db.Queryable<Order>().WhereClassByPrimaryKey(new Order() { Id=1 }).ToList(); //单个实体 var getAll = db.Queryable<Order>().WhereClassByPrimaryKey(List<Order>).ToList(); //支持集合 //根据实体类中的主键查询
适合多字段批量过滤
var getAll=db.Queryable<Order>().WhereColumns(new List<Dictionary<string, object>>).ToList()
Where中使用SqlFunc函数
var list=db.Queryable<Order>().Where(it=>SqlFunc.DateIsSame(it.Time,DateTime.Now)).ToList()
https://www.donet5.com/Home/Doc?typeId=1190
var list= db.Queryable<Student>() .Where(st=> SqlFunc.Subqueryable<School>().Where(sch=>sch.Id==st.SchoolId).Any()).ToList(); //更多用法看菜单:子查询
var list=db.Queryable<Student>().Where("id=@id",new {id=1}).ToList()
5.1.4.107-preview14+
//程序启动时配置 StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser); StaticConfig.DynamicExpressionParsingConfig = new ParsingConfig()//用到SqlFunc需要配置这个属性 { CustomTypeProvider = new SqlSugarTypeProvider()//这个类需要自已写在下面有 }; //导航属性动态 var list3 = db.Queryable<UnitPerson011>().Where("it", $"SqlFunc.Exists(it.Address.Id)").ToList(); //普通条件动态 var list4 = db.Queryable<UnitPerson011>().Where("it", $"it.Name=={"a"}").ToList(); //动态类+动态条件 var list5=db.QueryableByObject(typeof(UnitPerson011)).Where("it", $"it.Address.Id=={1}").ToList(); //扩展类型 public class SqlSugarTypeProvider : DefaultDynamicLinqCustomTypeProvider { public override HashSet<Type> GetCustomTypes() { var customTypes = base.GetCustomTypes(); customTypes.Add(typeof(SqlFunc));//识别SqlFunc return customTypes; } } /****参数说明****/ //硬编码 Id>1 FormattableString str = $"it.Id>1"; //固定字段+参数化变量 Id>@p=1 FormattableString str2 = FormattableStringFactory.Create("it.Id>{0}",1); //动态字段+参数化变量 Id>@p=1 var p="it.Id"; FormattableString str2 = FormattableStringFactory.Create(p+">{0}",1);
2016 © donet5.comApache Licence 2.0