From 2450324f28f5dea0d0ff877e55744af744407b37 Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@qq.com>
Date: Fri, 12 Aug 2022 12:07:17 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20IncludeByPropertyName?=
=?UTF-8?q?=20=E9=87=8D=E8=BD=BD=20then=20=E5=8F=82=E6=95=B0=EF=BC=9B#1214?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql.DbContext/FreeSql.DbContext.xml | 9 ++++
.../SqlServer/Curd/SqlServerSelectTest.cs | 8 ++++
.../Sqlite/Curd/SqliteSelectTest.cs | 8 +++-
FreeSql/Extensions/FreeSqlGlobalExtensions.cs | 34 +++++++++-----
FreeSql/FreeSql.xml | 4 +-
FreeSql/Interface/Curd/ISelect/ISelect1.cs | 2 +
.../SelectProvider/Select0ProviderReader.cs | 2 +-
.../SelectProvider/Select1Provider.cs | 45 +++++++++++++++++--
8 files changed, 95 insertions(+), 17 deletions(-)
diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml
index 537315e2..26522f10 100644
--- a/FreeSql.DbContext/FreeSql.DbContext.xml
+++ b/FreeSql.DbContext/FreeSql.DbContext.xml
@@ -800,5 +800,14 @@
+
+
+ 批量注入 Repository,可以参考代码自行调整
+
+
+
+
+
+
diff --git a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs
index 38f085d6..8ac1e221 100644
--- a/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs
+++ b/FreeSql.Tests/FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs
@@ -1562,6 +1562,14 @@ WHERE (((cast(a.[Id] as nvarchar(100))) in (SELECT TOP 10 b.[Title]
.Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
.ToList();
+ var tags333 = g.sqlserver.Select()
+ .IncludeByPropertyName("Tags",
+ then => then.IncludeByPropertyName("Parent").IncludeByPropertyName("Songs").IncludeByPropertyName("Tags"))
+ .IncludeByPropertyName("Parent")
+ .IncludeByPropertyName("Songs")
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+
var tags11 = g.sqlserver.Select()
.IncludeMany(a => a.Tags.Take(1))
.Include(a => a.Parent)
diff --git a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
index a71ff5cb..cfd4b5fc 100644
--- a/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
+++ b/FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
@@ -1368,7 +1368,7 @@ FROM ""TestTypeParentInfo_01"" a", asTableSubSql);
.ToList();
by1.IncludeByPropertyName(g.sqlite, "model2.childs", "model2111Idaaa=model2id");
- by1.IncludeByPropertyNameAsync(g.sqlite, "model2.childs", "model2111Idaaa=model2id");
+ by1.IncludeByPropertyNameAsync(g.sqlite, "model2.childs", "model2111Idaaa=model2id").Wait();
var t1 = g.sqlite.Select()
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
.Where(a => a.id <= model1.id)
@@ -1678,6 +1678,12 @@ FROM ""TestTypeParentInfo_01"" a", asTableSubSql);
.IncludeMany(a => a.Songs)
.Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
.ToList();
+ var tags3List = g.sqlite.Select()
+ .Include(a => a.Parent)
+ .Where(a => a.Id == tag1.Id || a.Id == tag2.Id)
+ .ToList();
+ tags3List.IncludeByPropertyName(g.sqlite, "Tags", then: then => then.IncludeByPropertyName("Parent").IncludeByPropertyName("Songs").IncludeByPropertyName("Tags"));
+ tags3List.IncludeByPropertyName(g.sqlite, "Songs");
var tags11 = g.sqlite.Select()
.IncludeMany(a => a.Tags.Take(1))
diff --git a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
index e964e345..35ae6016 100644
--- a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
+++ b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs
@@ -20,6 +20,12 @@ using System.Threading.Tasks;
public static partial class FreeSqlGlobalExtensions
{
+#if net40
+#else
+ static readonly Lazy _TaskReflectionResultPropertyLazy = new Lazy(() => typeof(Task).GetProperty("Result"));
+ internal static object GetTaskReflectionResult(this Task task) => _TaskReflectionResultPropertyLazy.Value.GetValue(task, new object[0]);
+#endif
+
#region Type 对象扩展方法
static Lazy> _dicIsNumberType = new Lazy>(() => new Dictionary
{
@@ -342,26 +348,26 @@ public static partial class FreeSqlGlobalExtensions
///
///
/// 选择一个集合或普通属性
- /// 设置临时的子集合关系映射,格式:子类属性=T1属性
+ /// 设置临时的子集合关系映射,格式:子类属性=T1属性,多组以逗号分割
/// 设置子集合只取条数
/// 设置子集合只查询部分字段
///
///
- public static List IncludeByPropertyName(this List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null) where T1 : class
+ public static List IncludeByPropertyName(this List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null, Expression>> then = null) where T1 : class
{
#if net40
- return IncludeByPropertyNameSyncOrAsync(false, list, orm, property, where, take, select);
+ return IncludeByPropertyNameSyncOrAsync(false, list, orm, property, where, take, select, then);
#else
- var task = IncludeByPropertyNameSyncOrAsync(false, list, orm, property, where, take, select);
+ var task = IncludeByPropertyNameSyncOrAsync(false, list, orm, property, where, take, select, then);
if (task.Exception != null) throw task.Exception.InnerException ?? task.Exception;
return task.Result;
#endif
}
#if net40
#else
- public static Task> IncludeByPropertyNameAsync(this List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null) where T1 : class
+ public static Task> IncludeByPropertyNameAsync(this List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null, Expression>> then = null) where T1 : class
{
- return IncludeByPropertyNameSyncOrAsync(true, list, orm, property, where, take, select);
+ return IncludeByPropertyNameSyncOrAsync(true, list, orm, property, where, take, select, then);
}
#endif
static
@@ -370,7 +376,7 @@ public static partial class FreeSqlGlobalExtensions
#else
async Task>
#endif
- IncludeByPropertyNameSyncOrAsync(bool isAsync, List list, IFreeSql orm, string property, string where = null, int take = 0, string select = null) where T1 : class
+ IncludeByPropertyNameSyncOrAsync(bool isAsync, List list, IFreeSql orm, string property, string where, int take, string select, Expression>> then) where T1 : class
{
if (orm.CodeFirst.IsAutoSyncStructure)
{
@@ -389,7 +395,7 @@ public static partial class FreeSqlGlobalExtensions
{
if (props.Length > 1)
IncludeByPropertyName(list, orm, string.Join(".", props.Take(props.Length - 1)));
- var imsel = IncludeManyByPropertyNameCommonGetSelect(orm, property, where, take, select);
+ var imsel = IncludeManyByPropertyNameCommonGetSelect(orm, property, where, take, select, then);
#if net40
imsel.SetList(list);
#else
@@ -438,7 +444,7 @@ public static partial class FreeSqlGlobalExtensions
});
return list;
}
- static Select1Provider IncludeManyByPropertyNameCommonGetSelect(IFreeSql orm, string property, string where = null, int take = 0, string select = null) where T1 : class
+ static Select1Provider IncludeManyByPropertyNameCommonGetSelect(IFreeSql orm, string property, string where, int take, string select, Expression>> then) where T1 : class
{
if (orm.CodeFirst.IsAutoSyncStructure)
{
@@ -506,12 +512,20 @@ public static partial class FreeSqlGlobalExtensions
memberInitExp = Expression.Lambda(reffuncType, memberInitExp, refparamExp);
exp = Expression.Call(refWhereMethod, exp, memberInitExp);
}
+ Delegate newthen = null;
+ if (then != null)
+ {
+ var newthenParm = Expression.Parameter(typeof(ISelect<>).MakeGenericType(reftb.Type));
+ var newthenLambdaBody = new Select1Provider