/// <summary> /// 返回动态类型的数据 /// </summary> /// <remarks> /// <para>不支持参数化!</para> /// <para>TSource别名 a</para> /// </remarks> /// <typeparam name="TSource">实体类型</typeparam> /// <typeparam name="TDto">业务模型类型</typeparam> /// <param name="source"></param> /// <param name="orm"></param> /// <param name="fields">指定字段</param> /// <param name="alias">指定别名</param> /// <returns></returns> public static List <dynamic> ToDynamic <TSource, TDto>(this ISelect <TSource> source, IFreeSql orm, IEnumerable <string> fields = null, string alias = "a") where TSource : class { var columns = orm.GetTableInfo <TSource>().ColumnsByCs; var _fields = string.Join( ",", fields == null ? columns.Select(o => $"{alias}.\"{o.Value.Attribute.Name}\"") : fields.Select(o => $"{alias}.\"{(columns.ContainsKey(o) ? columns[o].Attribute.Name : o)}\"")); source = source.AsAlias((type, old) => type == typeof(TDto) ? alias : old); return(orm.Ado.Query <dynamic>(source.ToSql(_fields))); }
/// <summary> /// 返回动态类型的数据 /// </summary> /// <remarks> /// <para>不支持参数化!</para> /// </remarks> /// <typeparam name="TSource">实体类型</typeparam> /// <typeparam name="TJoin0"></typeparam> /// <typeparam name="TDto">业务模型类型</typeparam> /// <param name="source"></param> /// <param name="orm"></param> /// <param name="fields">指定字段</param> /// <param name="alias">指定别名</param> /// <returns></returns> public static List <dynamic> ToDynamic <TSource, TJoin0, TDto>(this ISelect <TSource> source, IFreeSql orm, IEnumerable <string> fields = null, string alias = "a") where TSource : class { var table = orm.GetTableInfo <TSource>(); var table_join0 = orm.GetTableInfo <TJoin0>(); var _fields = string.Join( ",", fields == null ? table.Columns.Concat(table_join0.Columns).Select(o => $"{alias}.\"{o.Value.Attribute.Name}\"") : fields.Select(o => table.Columns.ContainsKey(o) || table_join0.Columns.ContainsKey(o) ? (table.Columns.ContainsKey(o) ? $"{alias}.\"{table.Columns[o].Attribute.Name}\"" : $"a__{table_join0.DbName}.\"{table_join0.Columns[o].Attribute.Name}\"") : $"{alias}.\"{o}\"")); source = source.AsAlias((type, old) => type == typeof(TDto) ? alias : old); return(orm.Ado.Query <dynamic>(source.ToSql(_fields))); }
/// <summary> /// SqlServer with(nolock) 查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="that"></param> /// <param name="lockType"></param> /// <param name="rule">多表查询时的锁规则</param> /// <returns></returns> public static ISelect <T> WithLock <T>(this ISelect <T> that, SqlServerLock lockType = SqlServerLock.NoLock, Dictionary <Type, bool> rule = null) => rule == null? that.AsAlias((type, old) => $"{old} With({lockType.ToString()})") : that.AsAlias((type, old) => rule.TryGetValue(type, out var trybool) && trybool ? $"{old} With({lockType.ToString()})" : old);