From f84abe1ab35f54be8180c1e8b0c28aa31cd0a099 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 3 May 2022 13:48:01 +0800 Subject: [PATCH] Update SetChildList --- .../QueryableProvider/MappingFieldsHelper.cs | 107 ++++++++++-------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs index fafee5c00..56760de16 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/MappingFieldsHelper.cs @@ -57,57 +57,10 @@ namespace SqlSugar public void SetChildList(EntityColumnInfo navColumnInfo,object item,List list, List mappingFieldsExpressions) { - if (item != null) + if (item != null) { //var expable =Expressionable.Create(); - foreach (var field in mappingFieldsExpressions) - { - InitMappingFieldsExpression(field); - } - var setList =new List(); - var count = mappingFieldsExpressions.Count; - if (count == 1) - { - setList=list.Where(it => GetWhereByIndex(item, mappingFieldsExpressions, it, 0)).ToList(); - } - else if (count == 2) - { - setList = list.Where(it => - GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 1) - ).ToList(); - } - else if (count == 3) - { - setList = list.Where(it => - GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 1) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 2) - ).ToList(); - } - else if (count == 4) - { - setList = list.Where(it => - GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 1) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 2) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 3) - ).ToList(); - } - else if (count == 5) - { - setList = list.Where(it => - GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 1) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 2) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 3) && - GetWhereByIndex(item, mappingFieldsExpressions, it, 4) - ).ToList(); - } - else - { - Check.ExceptionEasy("MappingField max value is 5", "MappingField最大数量不能超过5"); - } + List setList = GetSetList(item, list, mappingFieldsExpressions); //navColumnInfo.PropertyInfo.SetValue(); var instance = Activator.CreateInstance(navColumnInfo.PropertyInfo.PropertyType, true); var ilist = instance as IList; @@ -115,10 +68,64 @@ namespace SqlSugar { ilist.Add(value); } - navColumnInfo.PropertyInfo.SetValue(item,ilist); + navColumnInfo.PropertyInfo.SetValue(item, ilist); } } + private List GetSetList(object item, List list, List mappingFieldsExpressions) + { + foreach (var field in mappingFieldsExpressions) + { + InitMappingFieldsExpression(field); + } + var setList = new List(); + var count = mappingFieldsExpressions.Count; + if (count == 1) + { + setList = list.Where(it => GetWhereByIndex(item, mappingFieldsExpressions, it, 0)).ToList(); + } + else if (count == 2) + { + setList = list.Where(it => + GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 1) + ).ToList(); + } + else if (count == 3) + { + setList = list.Where(it => + GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 1) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 2) + ).ToList(); + } + else if (count == 4) + { + setList = list.Where(it => + GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 1) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 2) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 3) + ).ToList(); + } + else if (count == 5) + { + setList = list.Where(it => + GetWhereByIndex(item, mappingFieldsExpressions, it, 0) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 1) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 2) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 3) && + GetWhereByIndex(item, mappingFieldsExpressions, it, 4) + ).ToList(); + } + else + { + Check.ExceptionEasy("MappingField max value is 5", "MappingField最大数量不能超过5"); + } + + return setList; + } + private static bool GetWhereByIndex(object item, List mappingFieldsExpressions, object it,int index) { var left = mappingFieldsExpressions[index].LeftEntityColumn.PropertyInfo.GetValue(it).ObjToString();