mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 02:29:39 +08:00
Add Queryable.ToPivotList , DataTable , Json
This commit is contained in:
@@ -891,6 +891,19 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
return this.Context.Utilities.SerializeObject(this.ToPageList(pageIndex, pageSize, ref totalNumber), typeof(T));
|
return this.Context.Utilities.SerializeObject(this.ToPageList(pageIndex, pageSize, ref totalNumber), typeof(T));
|
||||||
}
|
}
|
||||||
|
public virtual DataTable ToPivotTable<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
|
||||||
|
{
|
||||||
|
return this.ToList().ToPivotTable(columnSelector,rowSelector,dataSelector);
|
||||||
|
}
|
||||||
|
public virtual List<dynamic> ToPivotList<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
|
||||||
|
{
|
||||||
|
return this.ToList().ToPivotList(columnSelector, rowSelector, dataSelector);
|
||||||
|
}
|
||||||
|
public virtual string ToPivotJson<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
|
||||||
|
{
|
||||||
|
var list= this.ToPivotList(columnSelector, rowSelector, dataSelector);
|
||||||
|
return this.Context.Utilities.SerializeObject(list);
|
||||||
|
}
|
||||||
public List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
public List<T> ToParentList(Expression<Func<T, object>> parentIdExpression, object primaryKeyValue)
|
||||||
{
|
{
|
||||||
List<T> result = new List<T>() { };
|
List<T> result = new List<T>() { };
|
||||||
|
@@ -166,6 +166,9 @@ namespace SqlSugar
|
|||||||
void AddQueue();
|
void AddQueue();
|
||||||
ISugarQueryable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
ISugarQueryable<T> IgnoreColumns(Expression<Func<T, object>> columns);
|
||||||
ISugarQueryable<T> IgnoreColumns(params string[] columns);
|
ISugarQueryable<T> IgnoreColumns(params string[] columns);
|
||||||
|
DataTable ToPivotTable<TColumn, TRow, TData>(Func<T, TColumn> columnSelector,Expression<Func<T, TRow>> rowSelector,Func<IEnumerable<T>, TData> dataSelector);
|
||||||
|
List<dynamic> ToPivotList<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector);
|
||||||
|
string ToPivotJson<TColumn, TRow, TData>(Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector);
|
||||||
}
|
}
|
||||||
public partial interface ISugarQueryable<T, T2> : ISugarQueryable<T>
|
public partial interface ISugarQueryable<T, T2> : ISugarQueryable<T>
|
||||||
{
|
{
|
||||||
|
@@ -192,6 +192,7 @@
|
|||||||
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerBlueCopy.cs" />
|
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerBlueCopy.cs" />
|
||||||
<Compile Include="SqlSugarClient.cs" />
|
<Compile Include="SqlSugarClient.cs" />
|
||||||
<Compile Include="Utilities\CallContext.cs" />
|
<Compile Include="Utilities\CallContext.cs" />
|
||||||
|
<Compile Include="Utilities\DataTableExtensions.cs" />
|
||||||
<Compile Include="Utilities\ReflectionExtensions.cs" />
|
<Compile Include="Utilities\ReflectionExtensions.cs" />
|
||||||
<Compile Include="Realization\MySql\CodeFirst\MySqlCodeFirst.cs" />
|
<Compile Include="Realization\MySql\CodeFirst\MySqlCodeFirst.cs" />
|
||||||
<Compile Include="Realization\MySql\DbFirst\MySqlDbFirst.cs" />
|
<Compile Include="Realization\MySql\DbFirst\MySqlDbFirst.cs" />
|
||||||
|
97
Src/Asp.Net/SqlSugar/Utilities/DataTableExtensions.cs
Normal file
97
Src/Asp.Net/SqlSugar/Utilities/DataTableExtensions.cs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Dynamic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
internal static class DataTableExtensions
|
||||||
|
{
|
||||||
|
public static DataTable ToPivotTable<T, TColumn, TRow, TData>(
|
||||||
|
this IEnumerable<T> source,
|
||||||
|
Func<T, TColumn> columnSelector,
|
||||||
|
Expression<Func<T, TRow>> rowSelector,
|
||||||
|
Func<IEnumerable<T>, TData> dataSelector)
|
||||||
|
{
|
||||||
|
DataTable table = new DataTable();
|
||||||
|
var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||||
|
table.Columns.Add(new DataColumn(rowName));
|
||||||
|
var columns = source.Select(columnSelector).Distinct();
|
||||||
|
|
||||||
|
foreach (var column in columns)
|
||||||
|
table.Columns.Add(new DataColumn(column.ToString()));
|
||||||
|
|
||||||
|
var rows = source.GroupBy(rowSelector.Compile())
|
||||||
|
.Select(rowGroup => new
|
||||||
|
{
|
||||||
|
Key = rowGroup.Key,
|
||||||
|
Values = columns.GroupJoin(
|
||||||
|
rowGroup,
|
||||||
|
c => c,
|
||||||
|
r => columnSelector(r),
|
||||||
|
(c, columnGroup) => dataSelector(columnGroup))
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach (var row in rows)
|
||||||
|
{
|
||||||
|
var dataRow = table.NewRow();
|
||||||
|
var items = row.Values.Cast<object>().ToList();
|
||||||
|
items.Insert(0, row.Key);
|
||||||
|
dataRow.ItemArray = items.ToArray();
|
||||||
|
table.Rows.Add(dataRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
public static List<dynamic> ToPivotList<T, TColumn, TRow, TData>(
|
||||||
|
this IEnumerable<T> source,
|
||||||
|
Func<T, TColumn> columnSelector,
|
||||||
|
Expression<Func<T, TRow>> rowSelector,
|
||||||
|
Func<IEnumerable<T>, TData> dataSelector)
|
||||||
|
{
|
||||||
|
|
||||||
|
var arr = new List<object>();
|
||||||
|
var cols = new List<string>();
|
||||||
|
String rowName = ((MemberExpression)rowSelector.Body).Member.Name;
|
||||||
|
var columns = source.Select(columnSelector).Distinct();
|
||||||
|
|
||||||
|
cols = (new[] { rowName }).Concat(columns.Select(x => x.ToString())).ToList();
|
||||||
|
|
||||||
|
|
||||||
|
var rows = source.GroupBy(rowSelector.Compile())
|
||||||
|
.Select(rowGroup => new
|
||||||
|
{
|
||||||
|
Key = rowGroup.Key,
|
||||||
|
Values = columns.GroupJoin(
|
||||||
|
rowGroup,
|
||||||
|
c => c,
|
||||||
|
r => columnSelector(r),
|
||||||
|
(c, columnGroup) => dataSelector(columnGroup))
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var row in rows)
|
||||||
|
{
|
||||||
|
var items = row.Values.Cast<object>().ToList();
|
||||||
|
items.Insert(0, row.Key);
|
||||||
|
var obj = GetAnonymousObject(cols, items);
|
||||||
|
arr.Add(obj);
|
||||||
|
}
|
||||||
|
return arr.ToList();
|
||||||
|
}
|
||||||
|
private static dynamic GetAnonymousObject(IEnumerable<string> columns, IEnumerable<object> values)
|
||||||
|
{
|
||||||
|
IDictionary<string, object> eo = new ExpandoObject() as IDictionary<string, object>;
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < columns.Count(); i++)
|
||||||
|
{
|
||||||
|
eo.Add(columns.ElementAt<string>(i), values.ElementAt<object>(i));
|
||||||
|
}
|
||||||
|
return eo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user