我想過濾包含複雜對象的DataTable或DaatView。在DataTable/DataView中過濾複雜對象
比方說,我有這樣的對象者
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}
}
現在我填一個DataView與Person對象的列表:
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);
}
讓我與Person對象的10列的數據視圖, evrey專欄有它的索引名稱:
IList<Person> personList = new List<Person>();
// Fill the list...
DataView dataSource = personList.ToObjectDataSource(10);
現在我想過濾這個DataVi例如,根據孩子的價值觀和表達方式,讓所有居住在'假客戶'的人都能獲得。
我試過「0.BillAddress.Street =‘Fakestreet’」(和或與其他列表達),但不工作..