mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 10:45:08 +08:00
!47 决ToPivotTable和ToPivotList的it=>it.Name问题
Merge pull request !47 from yanghs/master
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,43 @@ 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) =>
|
||||
{
|
||||
Action<DataRow, IGrouping<TRow, T>> action;
|
||||
if (string.IsNullOrEmpty(memberName))
|
||||
{
|
||||
action = (row, rowGroup) =>
|
||||
{
|
||||
var properties = rowGroup.Key.GetType().GetProperties();
|
||||
foreach (var item in properties)
|
||||
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
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<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,39 +83,48 @@ namespace SqlSugar
|
||||
Func<IEnumerable<T>, TData> dataSelector)
|
||||
{
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
return PivotHelper.ToPivotList(source, columnSelector, rowSelector, dataSelector);
|
||||
var memberName = string.Empty;
|
||||
|
||||
|
||||
var rowName = new List<string>();
|
||||
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())
|
||||
.Select(rowGroup =>
|
||||
Action<IDictionary<string, object>, IGrouping<TRow, T>> action;
|
||||
if (string.IsNullOrEmpty(memberName))
|
||||
{
|
||||
action = (row, rowGroup) =>
|
||||
{
|
||||
var anonymousType = rowGroup.Key.GetType();
|
||||
var properties = anonymousType.GetProperties();
|
||||
IDictionary<string, object> row = new ExpandoObject();
|
||||
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.OrderBy(it=> rowName.Contains(it.Key)?0:1);
|
||||
});
|
||||
var properties = rowGroup.Key.GetType().GetProperties();
|
||||
foreach (var item in properties)
|
||||
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
action = (row, rowGroup) => row[memberName] = rowGroup.Key;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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,43 @@ 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) =>
|
||||
{
|
||||
Action<DataRow, IGrouping<TRow, T>> action;
|
||||
if (string.IsNullOrEmpty(memberName))
|
||||
{
|
||||
action = (row, rowGroup) =>
|
||||
{
|
||||
var properties = rowGroup.Key.GetType().GetProperties();
|
||||
foreach (var item in properties)
|
||||
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
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<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,39 +83,48 @@ namespace SqlSugar
|
||||
Func<IEnumerable<T>, TData> dataSelector)
|
||||
{
|
||||
|
||||
if (rowSelector.Body is MemberExpression)
|
||||
return PivotHelper.ToPivotList(source, columnSelector, rowSelector, dataSelector);
|
||||
var memberName = string.Empty;
|
||||
|
||||
|
||||
var rowName = new List<string>();
|
||||
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())
|
||||
.Select(rowGroup =>
|
||||
Action<IDictionary<string, object>, IGrouping<TRow, T>> action;
|
||||
if (string.IsNullOrEmpty(memberName))
|
||||
{
|
||||
action = (row, rowGroup) =>
|
||||
{
|
||||
var anonymousType = rowGroup.Key.GetType();
|
||||
var properties = anonymousType.GetProperties();
|
||||
IDictionary<string, object> row = new ExpandoObject();
|
||||
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.OrderBy(it=> rowName.Contains(it.Key)?0:1);
|
||||
});
|
||||
var properties = rowGroup.Key.GetType().GetProperties();
|
||||
foreach (var item in properties)
|
||||
row[item.Name] = item.GetValue(rowGroup.Key, null);
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
action = (row, rowGroup) => row[memberName] = rowGroup.Key;
|
||||
}
|
||||
|
||||
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