diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs index b7c7dad14..da770526e 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs @@ -63,36 +63,46 @@ namespace OrmTest sch.TeacherList = db.Queryable().SetContext(teachera => teachera.SchoolId, () => sch.SchoolId, sch).ToList(); }); + var list2=db.Queryable() .Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include .Includes(x => x.Books) .ToList(); - - var list3= db.Queryable().Includes(x => x.BList).ToList(); + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new A1() { Id = 1, Name = "a1" }).ExecuteCommand(); + db.Insertable(new A1() { Id = 2, Name = "a2" }).ExecuteCommand(); + db.Insertable(new B1() { Id = 1, Name = "b1" }).ExecuteCommand(); + db.Insertable(new B1() { Id = 2, Name = "b2" }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId=1,BId=1 }).ExecuteCommand(); + db.Insertable(new ABMapping1() { AId = 2, BId = 2 }).ExecuteCommand(); + var list3= db.Queryable().Includes(x => x.BList).ToList(); } - public class ABMapping + public class ABMapping1 { - [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [SugarColumn(IsPrimaryKey = true )] public int AId { get; set; } public int BId { get; set; } } - public class A + public class A1 { - [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [SugarColumn(IsPrimaryKey = true )] public int Id { get; set; } public string Name { get; set; } - [Navigat(typeof(ABMapping),nameof(ABMapping.AId),nameof(ABMapping.BId))] - public List BList { get; set; } + [Navigat(typeof(ABMapping1),nameof(ABMapping1.AId),nameof(ABMapping1.BId))] + public List BList { get; set; } } - public class B + public class B1 { - [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [SugarColumn(IsPrimaryKey = true )] public int Id { get; set; } public string Name { get; set; } - [Navigat(typeof(ABMapping), nameof(ABMapping.BId), nameof(ABMapping.AId))] - public List BList { get; set; } + [Navigat(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))] + public List BList { get; set; } } public class StudentA @@ -112,7 +122,7 @@ namespace OrmTest [SugarColumn(IsPrimaryKey = true)] public int SchoolId { get; set; } public string SchoolName { get; set; } - [SugarColumn(IsIgnore = true)] + [Navigat(NavigatType.OneToMany,nameof(RoomA.SchoolId))] public List RoomList { get; set; } [SugarColumn(IsIgnore = true)] public List TeacherList { get; set; } diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs index a22c56d43..ca84d4522 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs @@ -23,7 +23,7 @@ namespace SqlSugar public List RootList { get; set; } //public QueryableProvider Queryable { get; set; } - //private List _preExpressionList = new List(); + private List _preExpressionList = new List(); //private Expression[] _expressions; //private List _list; //private EntityInfo _entityInfo; @@ -39,10 +39,19 @@ namespace SqlSugar private void ExecuteByLay(int i, Expression item) { + if (i == 1) { ExecuteByLay(item, RootList.Select(it => it as object).ToList(), SelectR1); } + else if(i == 2) + { + var memberExpression = ((_preExpressionList.Last() as LambdaExpression).Body as MemberExpression); + var navObjectName = memberExpression.Member.Name; + var list = RootList.Select(it =>(it.GetType().GetProperty(navObjectName).GetValue(it))).ToList(); + ExecuteByLay(item, list, SelectR2); + } + _preExpressionList.Add(item); } private void ExecuteByLay(Expression expression, List list, Func, List> selector) @@ -73,10 +82,45 @@ namespace SqlSugar } else { - + ManyToMany(list, selector, listItemEntity, navObjectNamePropety, navObjectNameColumnInfo); } } + private void ManyToMany(List list, Func, List> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo) + { + //var navEntity = navObjectNameColumnInfo.PropertyInfo.PropertyType.GetGenericArguments()[0]; + //var navEntityInfo = this.Context.EntityMaintenance.GetEntityInfo(navEntity); + //var navColumn = navEntityInfo.Columns.FirstOrDefault(it => it.PropertyName == navObjectNameColumnInfo.Navigat.Name); + ////var navType = navObjectNamePropety.PropertyType; + //var listItemPkColumn = listItemEntity.Columns.Where(it => it.IsPrimarykey).FirstOrDefault(); + + //var ids = list.Select(it => it.GetType().GetProperty(listItemPkColumn.PropertyName).GetValue(it)).Select(it => it == null ? "null" : it).Distinct().ToList(); + //List conditionalModels = new List(); + //conditionalModels.Add((new ConditionalModel() + //{ + // ConditionalType = ConditionalType.In, + // FieldName = navObjectNameColumnInfo.Navigat.Name, + // FieldValue = String.Join(",", ids), + // CSharpTypeName = listItemPkColumn.PropertyInfo.PropertyType.Name + //})); + //var navList = selector(this.Context.Queryable().AS(navEntityInfo.DbTableName).Where(conditionalModels)); + //if (navList.HasValue()) + //{ + // foreach (var item in list) + // { + // var setValue = navList + // .Where(x => navColumn.PropertyInfo.GetValue(x).ObjToString() == listItemPkColumn.PropertyInfo.GetValue(item).ObjToString()).ToList(); + // var instance = Activator.CreateInstance(navObjectNamePropety.PropertyType, true); + // var ilist = instance as IList; + // foreach (var value in setValue) + // { + // ilist.Add(value); + // } + // navObjectNamePropety.SetValue(item, instance); + // } + //} + } + private void OneToOne(List list, Func, List> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo) { var navColumn = listItemEntity.Columns.FirstOrDefault(it => it.PropertyName == navObjectNameColumnInfo.Navigat.Name); @@ -120,7 +164,7 @@ namespace SqlSugar })); var navList = selector(this.Context.Queryable().AS(navEntityInfo.DbTableName).Where(conditionalModels)); if (navList.HasValue()) - + { foreach (var item in list) { var setValue = navList @@ -131,17 +175,9 @@ namespace SqlSugar { ilist.Add(value); } - navObjectNamePropety.SetValue(item, instance); + navObjectNamePropety.SetValue(item, instance); } + } } - - //private void Lay2(List preExpressionList, List list) - //{ - - //} - //private void Lay3(List preExpressionList, List list) - //{ - - //} } }