/// <summary> /// 获取最大值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="thisValue"></param> /// <returns></returns> public static T Max <T>(this Taskable <T> thisValue) { var isClass = typeof(T).IsClass; if (isClass) { Check.Exception(isClass, "TaskExtensions.Max.thisValue T只能是为string和值类型。"); } return(thisValue.Tasks.Select(it => it.Result.Value).Max()); }
/// <summary> /// 如果序列中不包含任何元素,则返回默认值。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="thisValue"></param> /// <returns></returns> public static T ToFirstOrDefault <T>(this Taskable <T> thisValue) where T : class { var isClass = typeof(T).IsClass; if (!isClass) { Check.Exception(isClass, "TaskExtensions.ToSingle.thisValue T只能为class。"); } return(thisValue.MergeEntities <T>().FirstOrDefault()); }
/// <summary> /// 将Task中的结果集合并成List集成 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="thisValue"></param> /// <returns></returns> public static List <T> ToList <T>(this Taskable <T> thisValue) where T : class { var isClass = typeof(T).IsClass; if (!isClass) { Check.Exception(isClass, "TaskExtensions.ToList.thisValue T只能为class。"); } return(thisValue.MergeEntities <T>()); }
/// <summary> /// 将结果集合并到一个集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="thisValue"></param> /// <returns></returns> public static IEnumerable <DataRow> MergeTable <DataTable>(this Taskable <DataTable> thisValue) { var isDataTable = typeof(System.Data.DataTable) == typeof(DataTable); if (!isDataTable) { Check.Exception(isDataTable, "TaskExtensions.MergeTable.thisValue T只能为DataTable。"); } var reval = thisValue.Tasks.SelectMany(it => it.Result.DataTable.AsEnumerable()).ToList(); return(reval); }
/// <summary> /// 多线程请求所有数据库节点,同步汇总结果 /// </summary> /// <typeparam name="T">支持DataTable、实体类和值类型</typeparam> /// <param name="sql"></param> /// <param name="connectionStringList">连接字符串数组</param> /// <param name="whereObj">参数 例如: new { id="1",name="张三"}</param> /// <returns></returns> public Taskable <T> Taskable <T>(string sql, List <string> connectionStringList, object whereObj = null) { Taskable <T> reval = new Taskable <T>(); reval.Sql = sql; reval.WhereObj = whereObj; var tasks = new Task <CloudSearchResult <T> > [connectionStringList.Count]; for (int i = 0; i < tasks.Length; i++) { CloudPubMethod.TaskFactory <CloudSearchResult <T> >(ti => { string innerSql = sql; var connString = connectionStringList[ti]; var db = new SqlSugarClient(connString); SettingConnection(db); CloudSearchResult <T> itemReval = new CloudSearchResult <T>(); var isDataTable = typeof(T) == typeof(DataTable); var isClass = typeof(T).IsClass; if (innerSql.Contains("$:->connectionString<-:$")) { innerSql = innerSql.Replace("$:->connectionString<-:$", connString); } if (isDataTable) { itemReval.DataTable = db.GetDataTable(innerSql, whereObj); } else if (isClass) { itemReval.Entities = db.SqlQuery <T>(innerSql, whereObj); } else { var obj = db.GetScalar(sql, whereObj); if (obj == DBNull.Value) { itemReval.Value = default(T); } else { obj = Convert.ChangeType(obj, typeof(T)); itemReval.Value = (T)obj; } } itemReval.ConnectionString = connString; return(itemReval); }, tasks, i); } Task.WaitAll(tasks); reval.Tasks = tasks; return(reval); }
/// <summary> ///是否存在这条记录 /// </summary> /// <param name="thisValue"></param> /// <returns></returns> public static bool Any(this Taskable <int> thisValue) { return(thisValue.Tasks.Select(it => it.Result.Value).Count() > 0); }
/// <summary> /// 将结果集合并到一个集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="thisValue"></param> /// <returns></returns> public static List <T> MergeValue <T>(this Taskable <T> thisValue) { var reval = thisValue.Tasks.Select(it => it.Result.Value).ToList(); return(reval); }
/// <summary> /// 将结果集合并到一个集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="thisValue"></param> /// <returns></returns> public static List <T> MergeEntities <T>(this Taskable <T> thisValue) where T : class { var reval = thisValue.Tasks.SelectMany(it => it.Result.Entities).ToList(); return(reval); }
/// <summary> /// 获取count /// </summary> /// <param name="thisValue"></param> /// <returns></returns> public static int Count(this Taskable <int> thisValue) { return(thisValue.Tasks.Select(it => it.Result.Value).Sum()); }