我应该如何将以下LINQ代码转换为.Net 2.0代码?
由于公司政策,我无法将其升级到.Net 3.5。
GetErrorLog
将由ObjectDataSource
使用。
为了使下面的代码有效,我使用http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
中的System.Linq.Dynamic
public class errorLog
{
public string fileName { get; set; }
public string filePath { get; set; }
}
public static IQueryable<errorLog> GetErrorLog(int startRowIndex, int maximumRows, string sortExpression, string logPath)
{
if (string.IsNullOrEmpty(sortExpression))
{
sortExpression = "fileName";
}
string[] filePaths = Directory.GetFiles(logPath);
List<errorLog> files = new List<errorLog>();
foreach (string filePath in filePaths)
{
files.Add(new errorLog { fileName = Path.GetFileName(filePath), filePath = filePath });
}
return files.AsQueryable().OrderBy(sortExpression).Skip(startRowIndex).Take(maximumRows);
}
答案 0 :(得分:0)
我是这样做的,欢迎任何更好的答案。
public static DataTable GetErrorLog(int startRowIndex, int maximumRows, string sortExpression, string logPath)
{
if (string.IsNullOrEmpty(sortExpression))
{
sortExpression = "fileName DESC";
}
DataTable errorLog = GetErrorLogDataTable();
string[] filePaths = Directory.GetFiles(logPath);
foreach (string filePath in filePaths)
{
DataRow row = errorLog.NewRow();
row["fileName"] = Path.GetFileName(filePath);
row["filePath"] = filePath;
errorLog.Rows.Add(row);
}
DataView dataView = new DataView(errorLog);
dataView.Sort = sortExpression;
errorLog = dataView.ToTable();
DataTable pagedErrorLog = errorLog.Clone();
for (int i = startRowIndex; i < startRowIndex + maximumRows; i++)
{
if (i >= errorLog.Rows.Count)
{
break;
}
pagedErrorLog.ImportRow(errorLog.Rows[i]);
}
if (pagedErrorLog.Rows.Count <= 0)
{
return errorLog;
}
else
{
return pagedErrorLog;
}
}
private static DataTable GetErrorLogDataTable()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("fileName");
dataTable.Columns.Add("filePath");
return dataTable;
}