使用Dynamic.Linq,如何return查询DataTable
Using Dynamic.Linq, how to return query to DataTable
我正在使用 System.Linq.Dynamic
var query = this.testFormsDataSet.Consignment.AsEnumerable().GroupBy(groupBy, "it")
.Select(selectBy);
有了这个查询就OK了。
但现在我不明白如何将查询保存到 DataTable。我尝试使用 https://msdn.microsoft.com/en-us/library/bb669096(v=vs.110).aspx .
DataTable res = CustomLINQtoDataSetMethods.CopyToDataTable(query);
或
DataTable res = query.CopyToDataTable();
但是CopyToDataTable
还是不行。
请帮忙!!
基于Convert IEnumerable to DataTable
public static class DynamicLinqDataTableHelpers
{
public static DataTable ToDataTable(this IQueryable items)
{
Type type = items.ElementType;
// Create the result table, and gather all properties of a type
DataTable table = new DataTable(type.Name);
PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Add the properties as columns to the datatable
foreach (PropertyInfo prop in props)
{
Type propType = prop.PropertyType;
// Is it a nullable type? Get the underlying type
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propType = Nullable.GetUnderlyingType(propType);
}
table.Columns.Add(prop.Name, propType);
}
// Add the property values as rows to the datatable
foreach (object item in items)
{
var values = new object[props.Length];
if (item != null)
{
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
}
table.Rows.Add(values);
}
return table;
}
}
然后
DataTable res = query.ToDataTable();
主要区别在于,我使用 IQueryable
的 ElementType
属性 而不是 "extracting" 通过泛型的类型 T
现在...您似乎使用了适用于 IEnumerable
的 Dynamic LINQ 的一种奇怪变体...我已经调整了 ToDataTable
:
public static class DynamicLinqDataTableHelpers
{
public static DataTable ToDataTable(this IEnumerable items)
{
// Create the result table, and gather all properties of a type
DataTable table = new DataTable();
PropertyInfo[] props = null;
// Add the property values as rows to the datatable
foreach (object item in items)
{
if (props == null && item != null)
{
Type type = item.GetType();
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Add the properties as columns to the datatable
foreach (PropertyInfo prop in props)
{
Type propType = prop.PropertyType;
// Is it a nullable type? Get the underlying type
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propType = Nullable.GetUnderlyingType(propType);
}
table.Columns.Add(prop.Name, propType);
}
}
// When the column headers are defined, all the rows have
// their number of columns "fixed" to the right number
var values = new object[props != null ? props.Length : 0];
if (item != null)
{
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
}
table.Rows.Add(values);
}
return table;
}
}
我正在使用 System.Linq.Dynamic
var query = this.testFormsDataSet.Consignment.AsEnumerable().GroupBy(groupBy, "it")
.Select(selectBy);
有了这个查询就OK了。
但现在我不明白如何将查询保存到 DataTable。我尝试使用 https://msdn.microsoft.com/en-us/library/bb669096(v=vs.110).aspx .
DataTable res = CustomLINQtoDataSetMethods.CopyToDataTable(query);
或
DataTable res = query.CopyToDataTable();
但是CopyToDataTable
还是不行。
请帮忙!!
基于Convert IEnumerable to DataTable
public static class DynamicLinqDataTableHelpers
{
public static DataTable ToDataTable(this IQueryable items)
{
Type type = items.ElementType;
// Create the result table, and gather all properties of a type
DataTable table = new DataTable(type.Name);
PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Add the properties as columns to the datatable
foreach (PropertyInfo prop in props)
{
Type propType = prop.PropertyType;
// Is it a nullable type? Get the underlying type
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propType = Nullable.GetUnderlyingType(propType);
}
table.Columns.Add(prop.Name, propType);
}
// Add the property values as rows to the datatable
foreach (object item in items)
{
var values = new object[props.Length];
if (item != null)
{
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
}
table.Rows.Add(values);
}
return table;
}
}
然后
DataTable res = query.ToDataTable();
主要区别在于,我使用 IQueryable
ElementType
属性 而不是 "extracting" 通过泛型的类型 T
现在...您似乎使用了适用于 IEnumerable
的 Dynamic LINQ 的一种奇怪变体...我已经调整了 ToDataTable
:
public static class DynamicLinqDataTableHelpers
{
public static DataTable ToDataTable(this IEnumerable items)
{
// Create the result table, and gather all properties of a type
DataTable table = new DataTable();
PropertyInfo[] props = null;
// Add the property values as rows to the datatable
foreach (object item in items)
{
if (props == null && item != null)
{
Type type = item.GetType();
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Add the properties as columns to the datatable
foreach (PropertyInfo prop in props)
{
Type propType = prop.PropertyType;
// Is it a nullable type? Get the underlying type
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propType = Nullable.GetUnderlyingType(propType);
}
table.Columns.Add(prop.Name, propType);
}
}
// When the column headers are defined, all the rows have
// their number of columns "fixed" to the right number
var values = new object[props != null ? props.Length : 0];
if (item != null)
{
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
}
table.Rows.Add(values);
}
return table;
}
}