![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
/// 将集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public DataTable ToDataTable(List<GongGaoInfo> list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
// 定义筛选条件字符串和排序字符串
string strSort = "PublishDate DESC";
// 获得经过筛选和排序后的数据
DataRow[] resultRows = dt.Select(strWhere, strSort);
转自:URL地址搞忘记了,真的不好意思,向作者致敬!