mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
解决ToPivotTable和ToPivotList的it=>it.Name问题以及顺序问题
This commit is contained in:
@@ -36,56 +36,43 @@ namespace SqlSugar
|
|||||||
var columns = source.Select(columnSelector).Distinct();
|
var columns = source.Select(columnSelector).Distinct();
|
||||||
table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray());
|
table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray());
|
||||||
|
|
||||||
|
Action<DataRow, IGrouping<TRow, T>> action;
|
||||||
if (string.IsNullOrEmpty(memberName))
|
if (string.IsNullOrEmpty(memberName))
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) =>
|
||||||
.Select(rowGroup =>
|
{
|
||||||
{
|
var properties = rowGroup.Key.GetType().GetProperties();
|
||||||
var anonymousType = rowGroup.Key.GetType();
|
foreach (var item in properties)
|
||||||
var properties = anonymousType.GetProperties();
|
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) => row[memberName] = rowGroup.Key;
|
||||||
.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 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<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();
|
||||||
|
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,59 +83,48 @@ namespace SqlSugar
|
|||||||
Func<IEnumerable<T>, TData> dataSelector)
|
Func<IEnumerable<T>, TData> dataSelector)
|
||||||
{
|
{
|
||||||
|
|
||||||
var rowName = new List<string>();
|
|
||||||
var memberName = string.Empty;
|
var memberName = string.Empty;
|
||||||
|
|
||||||
if (rowSelector.Body is MemberExpression)
|
if (rowSelector.Body is MemberExpression)
|
||||||
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||||
|
|
||||||
var columns = source.Select(columnSelector).Distinct();
|
var columns = source.Select(columnSelector).Distinct();
|
||||||
|
|
||||||
|
Action<IDictionary<string, object>, IGrouping<TRow, T>> action;
|
||||||
if (string.IsNullOrEmpty(memberName))
|
if (string.IsNullOrEmpty(memberName))
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) =>
|
||||||
.Select(rowGroup =>
|
|
||||||
{
|
{
|
||||||
var anonymousType = rowGroup.Key.GetType();
|
var properties = rowGroup.Key.GetType().GetProperties();
|
||||||
var properties = anonymousType.GetProperties();
|
foreach (var item in properties)
|
||||||
IDictionary<string, object> row = new ExpandoObject();
|
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||||
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)
|
|
||||||
.SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null))
|
|
||||||
.ToList();
|
|
||||||
return row;
|
|
||||||
});
|
|
||||||
return rows;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) => row[memberName] = rowGroup.Key;
|
||||||
.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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rows = source.GroupBy(rowSelector.Compile())
|
||||||
|
.Select(rowGroup =>
|
||||||
|
{
|
||||||
|
IDictionary<string, object> row = new ExpandoObject();
|
||||||
|
action(row, rowGroup);
|
||||||
|
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)
|
||||||
|
.ToList();
|
||||||
|
return row;
|
||||||
|
});
|
||||||
|
return rows;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,6 @@ using System.Linq.Expressions;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Xml.Linq;
|
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
@@ -37,56 +36,43 @@ namespace SqlSugar
|
|||||||
var columns = source.Select(columnSelector).Distinct();
|
var columns = source.Select(columnSelector).Distinct();
|
||||||
table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray());
|
table.Columns.AddRange(columns.Select(x => new DataColumn(x?.ToString())).ToArray());
|
||||||
|
|
||||||
|
Action<DataRow, IGrouping<TRow, T>> action;
|
||||||
if (string.IsNullOrEmpty(memberName))
|
if (string.IsNullOrEmpty(memberName))
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) =>
|
||||||
.Select(rowGroup =>
|
{
|
||||||
{
|
var properties = rowGroup.Key.GetType().GetProperties();
|
||||||
var anonymousType = rowGroup.Key.GetType();
|
foreach (var item in properties)
|
||||||
var properties = anonymousType.GetProperties();
|
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) => row[memberName] = rowGroup.Key;
|
||||||
.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 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<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();
|
||||||
|
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,58 +83,48 @@ namespace SqlSugar
|
|||||||
Func<IEnumerable<T>, TData> dataSelector)
|
Func<IEnumerable<T>, TData> dataSelector)
|
||||||
{
|
{
|
||||||
|
|
||||||
var rowName = new List<string>();
|
|
||||||
var memberName = string.Empty;
|
var memberName = string.Empty;
|
||||||
|
|
||||||
if (rowSelector.Body is MemberExpression)
|
if (rowSelector.Body is MemberExpression)
|
||||||
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
memberName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||||
|
|
||||||
var columns = source.Select(columnSelector).Distinct();
|
var columns = source.Select(columnSelector).Distinct();
|
||||||
|
|
||||||
|
Action<IDictionary<string, object>, IGrouping<TRow, T>> action;
|
||||||
if (string.IsNullOrEmpty(memberName))
|
if (string.IsNullOrEmpty(memberName))
|
||||||
{
|
{
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
action = (row, rowGroup) =>
|
||||||
.Select(rowGroup =>
|
|
||||||
{
|
{
|
||||||
var anonymousType = rowGroup.Key.GetType();
|
var properties = rowGroup.Key.GetType().GetProperties();
|
||||||
var properties = anonymousType.GetProperties();
|
foreach (var item in properties)
|
||||||
IDictionary<string, object> row = new ExpandoObject();
|
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||||
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)
|
|
||||||
.SelectMany(x => properties, (x, y) => row[y.Name] = y.GetValue(rowGroup.Key, null))
|
|
||||||
.ToList();
|
|
||||||
return row;
|
|
||||||
});
|
|
||||||
return rows;
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
var rows = source.GroupBy(rowSelector.Compile())
|
{
|
||||||
.Select(rowGroup =>
|
action = (row, rowGroup) => row[memberName] = rowGroup.Key;
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rows = source.GroupBy(rowSelector.Compile())
|
||||||
|
.Select(rowGroup =>
|
||||||
|
{
|
||||||
|
IDictionary<string, object> row = new ExpandoObject();
|
||||||
|
action(row, rowGroup);
|
||||||
|
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)
|
||||||
|
.ToList();
|
||||||
|
return row;
|
||||||
|
});
|
||||||
|
return rows;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user