相对于【无实体查询相关的API】可以很好的弥补导航的缺陷,比如我想在Where或者Select中使用导航属性
无实体查询API : https://www.donet5.com/Home/Doc?typeId=2421
他们可以互补 1+1>2
//导航属性动态 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(); //动态字段+参数化变量 Id>1 var p="it.Id"; FormattableString str2 = FormattableStringFactory.Create(p+">{0}",1);
注意:单表一定要加上it=>不然会缓存有错误
//程序启动时配置 5.1.4.106 StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser); StaticConfig.DynamicExpressionParsingConfig = new ParsingConfig() { CustomTypeProvider = new SqlSugarTypeProvider() }; //扩展支持sqlfunc ,不支持subquery public class SqlSugarTypeProvider : DefaultDynamicLinqCustomTypeProvider { public override HashSet<Type> GetCustomTypes() { var customTypes = base.GetCustomTypes(); customTypes.Add(typeof(SqlFunc)); return customTypes; } }
//单个字段查询 List<int> list7= db.Queryable<UnitPerson011>().Select<int>("it", $"it.Id ",typeof(UnitPerson011), typeof(int)).ToList(); //集合查询 var list8 = db.Queryable<Order>().Select("it", $"new(it.Id as Id, it.Name)", typeof(Order)).ToList();
结合动态建类可很方便的无实现无代码,支持多种数据库
var shortNames=DynamicParameters.Create("x", typeof(Order), "u", typeof(OrderItem) ,"u2", typeof(OrderItem)); var list=db.QueryableByObject(typeof(Order), "x") .AddJoinInfo(typeof(OrderItem), DynamicParameters.Create("x", typeof(Order), "u", typeof(OrderItem)), $"x.Id==u.OrderId", JoinType.Left) .AddJoinInfo(typeof(OrderItem), DynamicParameters.Create("x", typeof(Order), "u", typeof(OrderItem), "u2", typeof(OrderItem)), $"x.Id==u2.OrderId", JoinType.Left) .Where(shortNames, $" x.Id == u.OrderId") .Select(shortNames, $"new (x.Name as Name,u.OrderId as Id)",typeof(ViewOrder)) .ToList(); //注意:联表不要有=> 这种语法 保证文档一样
用于动态表达式的参数
//硬编码 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);
https://www.donet5.com/Home/Doc?typeId=2562
2016 © donet5.comApache Licence 2.0