2016-07-27 5 views
0

Comment convertir les codes LINQ ci-dessous en codes .Net 2.0?Comment convertir les codes LINQ en codes .Net 2.0

Je ne peux pas mettre à niveau vers .Net 3.5 en raison de politique de l'entreprise certaines contraintes.

GetErrorLog sera utilisé par ObjectDataSource.

Pour le code ci-dessous pour travailler, j'utilise System.Linq.Dynamic de http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

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); 
    } 
+7

* Je ne peux pas mettre à niveau vers .Net 3.5 en raison de la politique de l'entreprise. * Obtenez une nouvelle société. –

+1

Qu'avez-vous essayé? Linq rend la vie facile mais ce n'est pas magique - Sort - vous avez de nombreux exemples pour le faire. Sautez - prenez juste à partir de l'index x. Prendre ... Prendre juste jusqu'à l'index –

+0

Montrer un exemple de 'sortExpression', pouvez-vous remplacer la méthode one par' GetErrorLogSortByName' et 'GetErrorLogSortByPath'? –

Répondre

0

Voici comment je le fais, mieux les réponses sont les bienvenus.

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; 
}