Browse Source

NavigatManager

pull/15/MERGE
sunkaixuan 3 years ago
parent
commit
77284d9bc6
  1. 36
      Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs
  2. 62
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs

36
Src/Asp.Net/SqlServerTest/UnitTest/UCustom012.cs

@ -63,36 +63,46 @@ namespace OrmTest
sch.TeacherList = db.Queryable<TeacherA>().SetContext(teachera => teachera.SchoolId, () => sch.SchoolId, sch).ToList();
});
var list2=db.Queryable<StudentA>()
.Includes(x => x.SchoolA, x => x.RoomList)//2个参数就是 then Include
.Includes(x => x.Books)
.ToList();
var list3= db.Queryable<A>().Includes(x => x.BList).ToList();
db.CodeFirst.InitTables<A1, B1, ABMapping1>();
db.DbMaintenance.TruncateTable<A1>();
db.DbMaintenance.TruncateTable<B1>();
db.DbMaintenance.TruncateTable<ABMapping1>();
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<A1>().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<B> BList { get; set; }
[Navigat(typeof(ABMapping1),nameof(ABMapping1.AId),nameof(ABMapping1.BId))]
public List<B1> 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<A> BList { get; set; }
[Navigat(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))]
public List<A1> 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<RoomA> RoomList { get; set; }
[SugarColumn(IsIgnore = true)]
public List<TeacherA> TeacherList { get; set; }

62
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/NavigatManager.cs

@ -23,7 +23,7 @@ namespace SqlSugar
public List<T> RootList { get; set; }
//public QueryableProvider<T> Queryable { get; set; }
//private List<Expression> _preExpressionList = new List<Expression>();
private List<Expression> _preExpressionList = new List<Expression>();
//private Expression[] _expressions;
//private List<T> _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<object> list, Func<ISugarQueryable<object>, List<object>> selector)
@ -73,10 +82,45 @@ namespace SqlSugar
}
else
{
ManyToMany(list, selector, listItemEntity, navObjectNamePropety, navObjectNameColumnInfo);
}
}
private void ManyToMany(List<object> list, Func<ISugarQueryable<object>, List<object>> 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<IConditionalModel> conditionalModels = new List<IConditionalModel>();
//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<object>().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<object> list, Func<ISugarQueryable<object>, List<object>> 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<object>().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<Expression> preExpressionList, List<T> list)
//{
//}
//private void Lay3(List<Expression> preExpressionList, List<T> list)
//{
//}
}
}

Loading…
Cancel
Save