mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
-
This commit is contained in:
@@ -90,6 +90,10 @@ namespace SqlSugar
|
||||
{
|
||||
return string.Join("UNION ALL \r\n", sqlList);
|
||||
}
|
||||
public virtual string GetUnionSql(List<string> sqlList)
|
||||
{
|
||||
return string.Join("UNION \r\n", sqlList);
|
||||
}
|
||||
public virtual void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
|
||||
{
|
||||
UtilMethods.RepairReplicationParameters(ref appendSql, parameters, addIndex);
|
||||
|
@@ -36,6 +36,7 @@ namespace SqlSugar
|
||||
string GetPackTable(string sql,string shortName);
|
||||
string GetDefaultShortName();
|
||||
string GetUnionAllSql(List<string> sqlList);
|
||||
string GetUnionSql(List<string> sqlList);
|
||||
void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex);
|
||||
}
|
||||
}
|
||||
|
@@ -396,6 +396,34 @@ namespace SqlSugar
|
||||
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
|
||||
return UnionAll(queryables.ToArray());
|
||||
}
|
||||
public virtual ISugarQueryable<T> Union<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
|
||||
{
|
||||
var sqlBuilder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
|
||||
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
|
||||
int i = 1;
|
||||
List<KeyValuePair<string, List<SugarParameter>>> allItems = new List<KeyValuePair<string, List<SugarParameter>>>();
|
||||
foreach (var item in queryables)
|
||||
{
|
||||
var sqlObj = item.ToSql();
|
||||
string sql = sqlObj.Key;
|
||||
UtilMethods.RepairReplicationParameters(ref sql, sqlObj.Value.ToArray(), i);
|
||||
if (sqlObj.Value.HasValue())
|
||||
allItems.Add(new KeyValuePair<string, List<SugarParameter>>(sql, sqlObj.Value));
|
||||
else
|
||||
allItems.Add(new KeyValuePair<string, List<SugarParameter>>(sql, new List<SugarParameter>()));
|
||||
i++;
|
||||
}
|
||||
var allSql = sqlBuilder.GetUnionSql(allItems.Select(it => it.Key).ToList());
|
||||
var allParameters = allItems.SelectMany(it => it.Value).ToArray();
|
||||
var resulut = this.Context.Queryable<ExpandoObject>().AS(UtilMethods.GetPackTable(allSql, "unionTable")).With(SqlWith.Null);
|
||||
resulut.AddParameters(allParameters);
|
||||
return resulut.Select<T>(sqlBuilder.SqlSelectAll);
|
||||
}
|
||||
public virtual ISugarQueryable<T> Union<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
|
||||
{
|
||||
Check.Exception(queryables.IsNullOrEmpty(), "Union.queryables is null ");
|
||||
return Union(queryables.ToArray());
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region SqlQueryable
|
||||
|
Reference in New Issue
Block a user