mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-11-10 19:35:09 +08:00
Update db.UnionAll
This commit is contained in:
@@ -188,6 +188,11 @@ namespace OrmTest.Demo
|
||||
var getDateIsSame= db.Queryable<Student>().Where(it => SqlFunc.DateIsSame(DateTime.Now,DateTime.Now,DateType.Hour)).ToList();
|
||||
|
||||
var getSqlList = db.Queryable<Student>().AS("(select * from student) t").ToList();
|
||||
|
||||
|
||||
var getUnionAllList = db.UnionAll(db.Queryable<Student>().Where(it => it.Id == 1), db.Queryable<Student>().Where(it => it.Id == 2)).ToList();
|
||||
|
||||
var getUnionAllList2 = db.UnionAll(db.Queryable<Student>(), db.Queryable<Student>()).ToList();
|
||||
}
|
||||
public static void Page()
|
||||
{
|
||||
|
||||
@@ -73,21 +73,11 @@ namespace SqlSugar
|
||||
}
|
||||
public virtual string GetPackTable(string sql, string shortName)
|
||||
{
|
||||
return string.Format(" ({0}) {1} ", sql, shortName);
|
||||
return UtilMethods.GetPackTable(sql,shortName);
|
||||
}
|
||||
public virtual void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
|
||||
{
|
||||
if (appendSql.IsValuable() && parameters.IsValuable())
|
||||
{
|
||||
foreach (var parameter in parameters.OrderByDescending(it=>it.ParameterName.Length))
|
||||
{
|
||||
//Compatible with.NET CORE parameters case
|
||||
var name = parameter.ParameterName;
|
||||
string newName = name + addIndex;
|
||||
appendSql = appendSql.Replace(name, newName);
|
||||
parameter.ParameterName = newName;
|
||||
}
|
||||
}
|
||||
UtilMethods.RepairReplicationParameters(ref appendSql,parameters,addIndex);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -299,23 +299,31 @@ namespace SqlSugar
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual List<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
|
||||
public virtual ISugarQueryable<T> UnionAll<T>(params ISugarQueryable<T>[] queryables) where T : class, new()
|
||||
{
|
||||
if (queryables.IsNullOrEmpty()) return new List<T>();
|
||||
List<T> result = new List<T>();
|
||||
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 addItems = item.ToList();
|
||||
if (addItems.IsValuable())
|
||||
var sqlObj = item.ToSql();
|
||||
string sql = sqlObj.Key;
|
||||
UtilMethods.RepairReplicationParameters(ref sql, sqlObj.Value.ToArray(), i);
|
||||
if (sqlObj.Value.IsValuable())
|
||||
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 = string.Join("UNION ALL \r\n", allItems.Select(it=>it.Key));
|
||||
var allParameters = allItems.SelectMany(it => it.Value).ToArray();
|
||||
var resulut = this.Queryable<ExpandoObject>().AS(UtilMethods.GetPackTable(allSql, "unionTable"));
|
||||
resulut.AddParameters(allParameters);
|
||||
return resulut.Select<T>("*");
|
||||
}
|
||||
public virtual ISugarQueryable<T> UnionAll<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
|
||||
{
|
||||
result.AddRange(addItems);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public virtual List<T> UnionAll<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
|
||||
{
|
||||
if (queryables.IsNullOrEmpty()) return new List<T>();
|
||||
Check.Exception(queryables.IsNullOrEmpty(), "UnionAll.queryables is null ");
|
||||
return UnionAll(queryables.ToArray());
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -54,5 +54,25 @@ namespace SqlSugar
|
||||
{
|
||||
return (T)Convert.ChangeType(obj, typeof(T));
|
||||
}
|
||||
|
||||
internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
|
||||
{
|
||||
if (appendSql.IsValuable() && parameters.IsValuable())
|
||||
{
|
||||
foreach (var parameter in parameters.OrderByDescending(it => it.ParameterName.Length))
|
||||
{
|
||||
//Compatible with.NET CORE parameters case
|
||||
var name = parameter.ParameterName;
|
||||
string newName = name + addIndex;
|
||||
appendSql = appendSql.Replace(name, newName);
|
||||
parameter.ParameterName = newName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static string GetPackTable(string sql, string shortName)
|
||||
{
|
||||
return string.Format(" ({0}) {1} ", sql, shortName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user