From 98da125373578555457877c854b3eba9ca3cfff4 Mon Sep 17 00:00:00 2001 From: YHS Date: Mon, 8 Jan 2024 14:26:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3ToPivotTable=E5=92=8CToPivotL?= =?UTF-8?q?ist=E7=9A=84it=3D>it.Name=E9=97=AE=E9=A2=98=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SqlSugar/Utilities/DataTableExtensions.cs | 142 +++++++---------- .../SqlSugar/Utilities/DataTableExtensions.cs | 148 ++++++++---------- 2 files changed, 121 insertions(+), 169 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Utilities/DataTableExtensions.cs b/Src/Asp.Net/SqlSugar/Utilities/DataTableExtensions.cs index 37b117ee7..bb5a2aea9 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/DataTableExtensions.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/DataTableExtensions.cs @@ -36,56 +36,43 @@ namespace SqlSugar var columns = source.Select(columnSelector).Distinct(); table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray()); + Action> action; if (string.IsNullOrEmpty(memberName)) { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => - { - var anonymousType = rowGroup.Key.GetType(); - var properties = anonymousType.GetProperties(); - var row = table.NewRow(); - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - - var dic = new Dictionary(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null)) - .ToArray(); - table.Rows.Add(row); - return row; - }) - .ToList(); + action = (row, rowGroup) => + { + var properties = rowGroup.Key.GetType().GetProperties(); + foreach (var item in properties) + row[item.Name] = item.GetValue(rowGroup.Key, null); + }; } else { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => - { - var row = table.NewRow(); - row[memberName] = rowGroup.Key; - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - - var dic = new Dictionary(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .ToArray(); - table.Rows.Add(row); - return row; - }).ToList(); + action = (row, rowGroup) => row[memberName] = rowGroup.Key; } + var rows = source.GroupBy(rowSelector.Compile()) + .Select(rowGroup => + { + var row = table.NewRow(); + action(row, rowGroup); + columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), + (c, columnGroup) => + { + + var dic = new Dictionary(); + if (c != null) + dic[c.ToString()] = dataSelector(columnGroup); + return dic; + }) + .SelectMany(x => x) + .Select(x => row[x.Key] = x.Value) + .ToArray(); + table.Rows.Add(row); + return row; + }) + .ToList(); + return table; } @@ -96,59 +83,48 @@ namespace SqlSugar Func, TData> dataSelector) { - var rowName = new List(); var memberName = string.Empty; if (rowSelector.Body is MemberExpression) memberName = ((MemberExpression)rowSelector.Body).Member.Name; var columns = source.Select(columnSelector).Distinct(); + + Action, IGrouping> action; if (string.IsNullOrEmpty(memberName)) { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => + action = (row, rowGroup) => { - var anonymousType = rowGroup.Key.GetType(); - var properties = anonymousType.GetProperties(); - IDictionary row = new ExpandoObject(); - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - IDictionary dic = new ExpandoObject(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null)) - .ToList(); - return row; - }); - return rows; + var properties = rowGroup.Key.GetType().GetProperties(); + foreach (var item in properties) + row[item.Name] = item.GetValue(rowGroup.Key, null); + }; } else { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => - { - IDictionary row = new ExpandoObject(); - row[memberName] = rowGroup.Key; - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - IDictionary dic = new ExpandoObject(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .ToList(); - return row; - }); - return rows; + action = (row, rowGroup) => row[memberName] = rowGroup.Key; } + + var rows = source.GroupBy(rowSelector.Compile()) + .Select(rowGroup => + { + IDictionary row = new ExpandoObject(); + action(row, rowGroup); + columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), + (c, columnGroup) => + { + var dic = new Dictionary(); + if (c != null) + dic[c.ToString()] = dataSelector(columnGroup); + return dic; + }) + .SelectMany(x => x) + .Select(x => row[x.Key] = x.Value) + .ToList(); + return row; + }); + return rows; + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/DataTableExtensions.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/DataTableExtensions.cs index 266de56eb..b31a3dd8a 100644 --- a/Src/Asp.NetCore2/SqlSugar/Utilities/DataTableExtensions.cs +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/DataTableExtensions.cs @@ -7,7 +7,6 @@ using System.Linq.Expressions; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; -using System.Xml.Linq; namespace SqlSugar { @@ -37,56 +36,43 @@ namespace SqlSugar var columns = source.Select(columnSelector).Distinct(); table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray()); + Action> action; if (string.IsNullOrEmpty(memberName)) { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => - { - var anonymousType = rowGroup.Key.GetType(); - var properties = anonymousType.GetProperties(); - var row = table.NewRow(); - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - - var dic = new Dictionary(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null)) - .ToArray(); - table.Rows.Add(row); - return row; - }) - .ToList(); + action = (row, rowGroup) => + { + var properties = rowGroup.Key.GetType().GetProperties(); + foreach (var item in properties) + row[item.Name] = item.GetValue(rowGroup.Key, null); + }; } else { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => - { - var row = table.NewRow(); - row[memberName] = rowGroup.Key; - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - - var dic = new Dictionary(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .ToArray(); - table.Rows.Add(row); - return row; - }).ToList(); + action = (row, rowGroup) => row[memberName] = rowGroup.Key; } + var rows = source.GroupBy(rowSelector.Compile()) + .Select(rowGroup => + { + var row = table.NewRow(); + action(row, rowGroup); + columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), + (c, columnGroup) => + { + + var dic = new Dictionary(); + if (c != null) + dic[c.ToString()] = dataSelector(columnGroup); + return dic; + }) + .SelectMany(x => x) + .Select(x => row[x.Key] = x.Value) + .ToArray(); + table.Rows.Add(row); + return row; + }) + .ToList(); + return table; } @@ -97,58 +83,48 @@ namespace SqlSugar Func, TData> dataSelector) { - var rowName = new List(); var memberName = string.Empty; if (rowSelector.Body is MemberExpression) memberName = ((MemberExpression)rowSelector.Body).Member.Name; var columns = source.Select(columnSelector).Distinct(); + + Action, IGrouping> action; if (string.IsNullOrEmpty(memberName)) { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => + action = (row, rowGroup) => { - var anonymousType = rowGroup.Key.GetType(); - var properties = anonymousType.GetProperties(); - IDictionary row = new ExpandoObject(); - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - IDictionary dic = new ExpandoObject(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null)) - .ToList(); - return row; - }); - return rows; + var properties = rowGroup.Key.GetType().GetProperties(); + foreach (var item in properties) + row[item.Name] = item.GetValue(rowGroup.Key, null); + }; } - else { - var rows = source.GroupBy(rowSelector.Compile()) - .Select(rowGroup => - { - IDictionary row = new ExpandoObject(); - row[memberName] = rowGroup.Key; - columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), - (c, columnGroup) => - { - IDictionary dic = new ExpandoObject(); - if (c != null) - dic[c.ToString()] = dataSelector(columnGroup); - return dic; - }) - .SelectMany(x => x) - .Select(x => row[x.Key] = x.Value) - .ToList(); - return row; - }); - return rows; + else + { + action = (row, rowGroup) => row[memberName] = rowGroup.Key; } + + var rows = source.GroupBy(rowSelector.Compile()) + .Select(rowGroup => + { + IDictionary row = new ExpandoObject(); + action(row, rowGroup); + columns.GroupJoin(rowGroup, c => c, r => columnSelector(r), + (c, columnGroup) => + { + var dic = new Dictionary(); + if (c != null) + dic[c.ToString()] = dataSelector(columnGroup); + return dic; + }) + .SelectMany(x => x) + .Select(x => row[x.Key] = x.Value) + .ToList(); + return row; + }); + return rows; + } } -} +} \ No newline at end of file