过滤 DataTable/DataView 中的复杂对象
Filter complex objects in DataTable/DataView
我想筛选包含复杂对象的 DataTable 或 DaatView。
假设我有这个对象 Person
public class Person{
public int Id{get; set;}
public int Age{get; set;}
public strng Name{get; set;}
public Address BillAddress{get; set;}
}
public class Address{
public string
Town{get; set}
public string Street{get; set}
public int Number{get; set}
}
现在我用 Person 对象列表填充 DataView:
public static DataView ToObjectDataView<T>(this IList<T> list, int countOfColumns)
{
if (list == null || countOfColumns < 1)
{
return null;
}
int columns = countOfColumns;
DataTable dataTable = new DataTable();
for (int currentCol = 0; currentCol != columns; currentCol++)
{
DataColumn column = new DataColumn(currentCol.ToString(), typeof(T));
dataTable.Columns.Add(column);
}
DataRow row = null;
int currentColumn = 0;
for (int index = 0; index < list.Count; index++)
{
if (list[index] == null)
{
continue;
}
if (Equals(null, row))
row = dataTable.NewRow();
row[currentColumn] = list[index];
currentColumn++;
if (currentColumn == columns)
{
dataTable.Rows.Add(row);
row = null;
currentColumn = 0;
}
}
//Verarbeitung der letzten Zeile
if (!Equals(null, row))
{
dataTable.Rows.Add(row);
}
return new DataView(dataTable);
}
所以我得到一个包含 10 列 Person 对象的 DataView,每个列都有其索引的名称:
IList<Person> personList = new List<Person>();
// Fill the list...
DataView dataSource = personList.ToObjectDataSource(10);
现在我想使用表达式根据子值过滤此 DataView,例如,获取居住在 'Fakestreet'.
中的所有人
我试过“0.BillAddress.Street = 'Fakestreet'”(和/或表达式与其余列)但那不起作用..
这是部分解决方案,因为我没有找到直接的方法。
使用 DataTable AsEnumerable 扩展和动态 linq 过滤器(System.Linq.Dynamic(也适用于 .Net 3.5))
// Filter directly the List
public static List<T> FilterByLinqExpression<T>(this IList<T> list, string linqFilterExpression)
{
var result = list.AsQueryable().Where(linqFilterExpression);
return result.ToList<T>();
}
所以你可以这样称呼所有住在街道名称中有 'Avenue' 的人:
IList<Person> personList = new List<Person>();
// Fill the list...
var filteredValues = personList.FilterByLinqExpression("((BillAddress.Street).Contains(\"Avenue\"))");
DataView dataSource = filteredValues .ToObjectDataSource(10);
我用它来过滤例如用于在 DevExpress ASPxGridView 中显示的复杂对象。顺便说一句,他们有一个从他们的过滤器表达式到不同过滤器表达式的自动转换器,在我的例子中是 'CriteriaToWhereClauseHelper.GetDynamicLinqWhere()' 将给定的过滤器表达式转换为动态 linq 表达式。
我想筛选包含复杂对象的 DataTable 或 DaatView。
假设我有这个对象 Person
public class Person{
public int Id{get; set;}
public int Age{get; set;}
public strng Name{get; set;}
public Address BillAddress{get; set;}
}
public class Address{
public string
Town{get; set}
public string Street{get; set}
public int Number{get; set}
}
现在我用 Person 对象列表填充 DataView:
public static DataView ToObjectDataView<T>(this IList<T> list, int countOfColumns)
{
if (list == null || countOfColumns < 1)
{
return null;
}
int columns = countOfColumns;
DataTable dataTable = new DataTable();
for (int currentCol = 0; currentCol != columns; currentCol++)
{
DataColumn column = new DataColumn(currentCol.ToString(), typeof(T));
dataTable.Columns.Add(column);
}
DataRow row = null;
int currentColumn = 0;
for (int index = 0; index < list.Count; index++)
{
if (list[index] == null)
{
continue;
}
if (Equals(null, row))
row = dataTable.NewRow();
row[currentColumn] = list[index];
currentColumn++;
if (currentColumn == columns)
{
dataTable.Rows.Add(row);
row = null;
currentColumn = 0;
}
}
//Verarbeitung der letzten Zeile
if (!Equals(null, row))
{
dataTable.Rows.Add(row);
}
return new DataView(dataTable);
}
所以我得到一个包含 10 列 Person 对象的 DataView,每个列都有其索引的名称:
IList<Person> personList = new List<Person>();
// Fill the list...
DataView dataSource = personList.ToObjectDataSource(10);
现在我想使用表达式根据子值过滤此 DataView,例如,获取居住在 'Fakestreet'.
中的所有人我试过“0.BillAddress.Street = 'Fakestreet'”(和/或表达式与其余列)但那不起作用..
这是部分解决方案,因为我没有找到直接的方法。
使用 DataTable AsEnumerable 扩展和动态 linq 过滤器(System.Linq.Dynamic(也适用于 .Net 3.5))
// Filter directly the List
public static List<T> FilterByLinqExpression<T>(this IList<T> list, string linqFilterExpression)
{
var result = list.AsQueryable().Where(linqFilterExpression);
return result.ToList<T>();
}
所以你可以这样称呼所有住在街道名称中有 'Avenue' 的人:
IList<Person> personList = new List<Person>();
// Fill the list...
var filteredValues = personList.FilterByLinqExpression("((BillAddress.Street).Contains(\"Avenue\"))");
DataView dataSource = filteredValues .ToObjectDataSource(10);
我用它来过滤例如用于在 DevExpress ASPxGridView 中显示的复杂对象。顺便说一句,他们有一个从他们的过滤器表达式到不同过滤器表达式的自动转换器,在我的例子中是 'CriteriaToWhereClauseHelper.GetDynamicLinqWhere()' 将给定的过滤器表达式转换为动态 linq 表达式。