mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 10:45:08 +08:00
解决 ToPivotTable和ToPivotList 的 it=>ite.Name问题
This commit is contained in:
@@ -19,15 +19,15 @@ namespace SqlSugar
|
||||
Func<IEnumerable<T>, TData> dataSelector)
|
||||
{
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
return PivotHelper.ToPivotTable(source, columnSelector, rowSelector, dataSelector);
|
||||
|
||||
|
||||
DataTable table = new DataTable();
|
||||
|
||||
var rowName = new List<string>();
|
||||
var memberName = string.Empty;
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
rowName.Add(((MemberExpression)rowSelector.Body).Member.Name);
|
||||
{
|
||||
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||
rowName.Add(memberName);
|
||||
}
|
||||
else
|
||||
rowName.AddRange(((NewExpression)rowSelector.Body).Arguments.Select(it => it as MemberExpression).Select(it => it.Member.Name));
|
||||
|
||||
@@ -36,29 +36,56 @@ namespace SqlSugar
|
||||
var columns = source.Select(columnSelector).Distinct();
|
||||
table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray());
|
||||
|
||||
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) =>
|
||||
{
|
||||
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<string, object>();
|
||||
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();
|
||||
}
|
||||
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<string, object>();
|
||||
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();
|
||||
}
|
||||
|
||||
var dic = new Dictionary<string, object>();
|
||||
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();
|
||||
return table;
|
||||
}
|
||||
|
||||
@@ -69,19 +96,16 @@ namespace SqlSugar
|
||||
Func<IEnumerable<T>, TData> dataSelector)
|
||||
{
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
return PivotHelper.ToPivotList(source, columnSelector, rowSelector, dataSelector);
|
||||
|
||||
|
||||
var rowName = new List<string>();
|
||||
var memberName = string.Empty;
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
rowName.Add(((MemberExpression)rowSelector.Body).Member.Name);
|
||||
else
|
||||
rowName.AddRange(((NewExpression)rowSelector.Body).Arguments.Select(it => it as MemberExpression).Select(it => it.Member.Name));
|
||||
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||
|
||||
var columns = source.Select(columnSelector).Distinct();
|
||||
|
||||
var rows = source.GroupBy(rowSelector.Compile())
|
||||
if (string.IsNullOrEmpty(memberName))
|
||||
{
|
||||
var rows = source.GroupBy(rowSelector.Compile())
|
||||
.Select(rowGroup =>
|
||||
{
|
||||
var anonymousType = rowGroup.Key.GetType();
|
||||
@@ -99,9 +123,32 @@ namespace SqlSugar
|
||||
.Select(x => row[x.Key] = x.Value)
|
||||
.SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null))
|
||||
.ToList();
|
||||
return row.OrderBy(it=> rowName.Contains(it.Key)?0:1);
|
||||
return row;
|
||||
});
|
||||
return rows;
|
||||
return rows;
|
||||
}
|
||||
else
|
||||
{
|
||||
var rows = source.GroupBy(rowSelector.Compile())
|
||||
.Select(rowGroup =>
|
||||
{
|
||||
IDictionary<string, object> row = new ExpandoObject();
|
||||
row[memberName] = rowGroup.Key;
|
||||
columns.GroupJoin(rowGroup, c => c, r => columnSelector(r),
|
||||
(c, columnGroup) =>
|
||||
{
|
||||
IDictionary<string, object> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -7,6 +7,7 @@ using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
@@ -19,15 +20,15 @@ namespace SqlSugar
|
||||
Func<IEnumerable<T>, TData> dataSelector)
|
||||
{
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
return PivotHelper.ToPivotTable(source, columnSelector, rowSelector, dataSelector);
|
||||
|
||||
|
||||
DataTable table = new DataTable();
|
||||
|
||||
var rowName = new List<string>();
|
||||
var memberName = string.Empty;
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
rowName.Add(((MemberExpression)rowSelector.Body).Member.Name);
|
||||
{
|
||||
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||
rowName.Add(memberName);
|
||||
}
|
||||
else
|
||||
rowName.AddRange(((NewExpression)rowSelector.Body).Arguments.Select(it => it as MemberExpression).Select(it => it.Member.Name));
|
||||
|
||||
@@ -36,29 +37,56 @@ namespace SqlSugar
|
||||
var columns = source.Select(columnSelector).Distinct();
|
||||
table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray());
|
||||
|
||||
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) =>
|
||||
{
|
||||
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<string, object>();
|
||||
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();
|
||||
}
|
||||
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<string, object>();
|
||||
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();
|
||||
}
|
||||
|
||||
var dic = new Dictionary<string, object>();
|
||||
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();
|
||||
return table;
|
||||
}
|
||||
|
||||
@@ -69,19 +97,16 @@ namespace SqlSugar
|
||||
Func<IEnumerable<T>, TData> dataSelector)
|
||||
{
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
return PivotHelper.ToPivotList(source, columnSelector, rowSelector, dataSelector);
|
||||
|
||||
|
||||
var rowName = new List<string>();
|
||||
var memberName = string.Empty;
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
rowName.Add(((MemberExpression)rowSelector.Body).Member.Name);
|
||||
else
|
||||
rowName.AddRange(((NewExpression)rowSelector.Body).Arguments.Select(it => it as MemberExpression).Select(it => it.Member.Name));
|
||||
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||
|
||||
var columns = source.Select(columnSelector).Distinct();
|
||||
|
||||
var rows = source.GroupBy(rowSelector.Compile())
|
||||
if (string.IsNullOrEmpty(memberName))
|
||||
{
|
||||
var rows = source.GroupBy(rowSelector.Compile())
|
||||
.Select(rowGroup =>
|
||||
{
|
||||
var anonymousType = rowGroup.Key.GetType();
|
||||
@@ -99,9 +124,31 @@ namespace SqlSugar
|
||||
.Select(x => row[x.Key] = x.Value)
|
||||
.SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null))
|
||||
.ToList();
|
||||
return row.OrderBy(it=> rowName.Contains(it.Key)?0:1);
|
||||
return row;
|
||||
});
|
||||
return rows;
|
||||
return rows;
|
||||
}
|
||||
else {
|
||||
var rows = source.GroupBy(rowSelector.Compile())
|
||||
.Select(rowGroup =>
|
||||
{
|
||||
IDictionary<string, object> row = new ExpandoObject();
|
||||
row[memberName] = rowGroup.Key;
|
||||
columns.GroupJoin(rowGroup, c => c, r => columnSelector(r),
|
||||
(c, columnGroup) =>
|
||||
{
|
||||
IDictionary<string, object> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user