Code optimization

This commit is contained in:
skx 2021-01-18 00:37:58 +08:00
parent 2b4c67e45a
commit 7af88bc649
2 changed files with 57 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using System;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -32,6 +33,51 @@ namespace OrmTest
.Where(it => it.Items.Any())
.ToList();
var sql=Db.Queryable<Order>().AS("[order]").ToList();
var sql1 = Db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
JoinType.Left, o.Id == i.OrderId,
JoinType.Left, c.Id == o.CustomId
))
.AS("[aa]")
.AS<OrderItem>("[xx]")
.AS<Custom>("[yy]")
.Select<ViewOrder>().ToSql().Key;
if (!sql1.Contains("[aa]") || !sql1.Contains("[xx]") || !sql1.Contains("[yy]"))
{
throw new Exception("unit queryable2 ");
}
var sql2 = Db.Queryable<OrderItem>().AS("[zz]").ToSql().Key;
if (sql2 != "SELECT [ItemId],[OrderId],[Price],[CreateTime] FROM [zz] ")
{
throw new Exception("unit queryable2 ");
}
Db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
JoinType.Left, o.Id == i.OrderId,
JoinType.Left, c.Id == o.CustomId
))
.AS("[order]")
.AS<OrderItem>("[orderdetail]")
.AS<Custom>("[custom]")
.Select<ViewOrder>().ToList();
Db.Queryable<object>().AS("[order]").Select("*").ToList();
var qu1=Db.Queryable<Order>().Select(it => new
{
id = it.Id
}).MergeTable().Select<Order>();
var qu2 = Db.Queryable<Order>().Select(it => new
{
id = it.Id,
name=it.Name
}).MergeTable().Select<Order>();
var list=Db.Queryable(qu1, qu2,JoinType.Left, (x, y) => x.Id == y.Id).Select((x,y) => new
{
id1=x.Id,
name=y.Name
}).ToList();
}
}
}

View File

@ -358,10 +358,19 @@ namespace SqlSugar
public virtual string ToJoinString(JoinQueryInfo joinInfo)
{
var name = joinInfo.TableName;
if (this.AsTables.Any(it => it.Key == name))
{
if (this.Context.MappingTables != null && this.Context.MappingTables.Any(it => it.DbTableName == name))
{
name = this.Context.MappingTables.First(it => it.DbTableName == name).EntityName;
}
name = this.AsTables.First(it => it.Key == name).Value;
}
return string.Format(
this.JoinTemplate,
joinInfo.JoinType.ToString() + UtilConstants.Space,
Builder.GetTranslationTableName(joinInfo.TableName) + UtilConstants.Space,
Builder.GetTranslationTableName(name) + UtilConstants.Space,
joinInfo.ShortName + UtilConstants.Space + (TableWithString == SqlWith.Null ? " " : TableWithString),
joinInfo.JoinWhere);
}