From bf84bf0fcbc0636922c165ba06c6f4c81ba345a3 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Tue, 22 Sep 2020 22:45:34 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20IAdo.Query\<=E5=8C=BF?= =?UTF-8?q?=E5=90=8D=E7=B1=BB\>(sql)=20=E9=94=99=E8=AF=AF=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 9 --------- FreeSql/Internal/UtilsExpressionTree.cs | 21 ++++++++++++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index 743835e4..9c6cd88b 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -532,14 +532,5 @@ - - - 批量注入 Repository,可以参考代码自行调整 - - - - - - diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 83c5c442..148ac6c4 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -1463,7 +1463,7 @@ namespace FreeSql.Internal var name = row2.GetName(a); //expando[name] = row2.GetValue(a); if (expandodic.ContainsKey(name)) continue; - expandodic.Add(name, Utils.InternalDataReaderGetValue(_commonUtils, row2, a)); + expandodic.Add(name, Utils.InternalDataReaderGetValue(commonUtils2, row2, a)); } //expando = expandodic; return new RowInfo(expandodic, fc); @@ -1471,6 +1471,8 @@ namespace FreeSql.Internal return dynamicFunc;// Expression.Lambda>(null); } + if (type.IsAnonymousType()) return ExecuteArrayRowReadAnonymousType; + //类注入属性 var typetb = GetTableByEntity(type, _commonUtils); var retExp = Expression.Variable(type, "ret"); @@ -1596,12 +1598,13 @@ namespace FreeSql.Internal var propIndex = 0; foreach (var prop in props) { - if (typetb.ColumnsByCsIgnore.ContainsKey(prop.Name)) + if (typetb?.ColumnsByCsIgnore.ContainsKey(prop.Name) == true) { ++propIndex; continue; } - var readType = typetb.ColumnsByCs.TryGetValue(prop.Name, out var trycol) ? trycol.Attribute.MapType : prop.PropertyType; + ColumnInfo trycol = null; + var readType = typetb?.ColumnsByCs.TryGetValue(prop.Name, out trycol) == true ? trycol.Attribute.MapType : prop.PropertyType; var ispkExp = new List(); var propGetSetMethod = prop.GetSetMethod(true); @@ -1700,6 +1703,18 @@ namespace FreeSql.Internal return func(typeOrg, indexes, row, dataIndex, _commonUtils); } + internal static RowInfo ExecuteArrayRowReadAnonymousType(Type type2, int[] indexes2, DbDataReader row2, int dataindex2, CommonUtils commonUtils2) + { + var ctor = type2.InternalGetTypeConstructor0OrFirst(); + var ctorParms = new object[ctor.GetParameters().Length]; + if (indexes2?.Length != ctorParms.Length) + indexes2 = ctor.GetParameters().Select(c => row2.GetOrdinal(c.Name)).ToArray(); + + for (var c = 0; c < ctorParms.Length; c++) + ctorParms[c] = Utils.InternalDataReaderGetValue(commonUtils2, row2, indexes2[c]); + return new RowInfo(ctor.Invoke(ctorParms), ctorParms.Length); + } + internal static MethodInfo MethodExecuteArrayRowReadClassOrTuple = typeof(Utils).GetMethod("ExecuteArrayRowReadClassOrTuple", BindingFlags.Static | BindingFlags.NonPublic); internal static MethodInfo MethodGetDataReaderValue = typeof(Utils).GetMethod("GetDataReaderValue", BindingFlags.Static | BindingFlags.NonPublic);