2016-07-27 8 views
0

私のデータテーブルからのパラメータに基づいてクエリを作成しようとしています。私はエラーを取得し続け、基本的に1つのパラメータが異なるそれぞれのcase文でlinqクエリを再作成することを除いて、次のようなことを実行しようとしています。 OrderByまたはOrderByDecending。linqクエリをパラメータに基づいて構築する

パラメータに基づいてクエリを作成する方法はありますか?

public JsonResult IndexData(DTParameterViewModel param) 
    { 
      IEnumerable<DataTableRowViewModel> rows = (from j in jobs select j) 
         .Skip(param.Start) 
         .Take(param.Length) 
         .AsEnumerable() 
         .Select(j => new DataTableRowViewModel 
         { 
          Id = j.Id, 
          ArrivalDate = j.ArrivalDate.ToString(Constants.DATE_FORMAT), 
          DueDate = j.DueDate?.ToString(Constants.DATE_TIME_FORMAT), 
          Contact = j.Contact, 
          Priority = j.Priority.GetDisplayName(), 
          JobType = j.JobType.GetDisplayName(), 
          SapDate = j.SapDate.ToString() 
         }); 

     foreach(DTOrder order in param.Order) 
     { 
      ascDesc = order.Dir; 

      switch (order.Column) 
      { 
       case 0: 
        orderCol = 0; 
        colName = "Id"; 

        if (ascDesc == "desc") 
        { 
         rows = (from j in jobs select j) 
         .OrderByDescending(j => j.Id); 

        } 
        else 
        { 
         rows = (from j in jobs select j) 
         .OrderBy(j => j.Id) 
        } 

        break; 

       case 1: 
        orderCol = 1; 
        colName = "ArrivalDate"; 

        if (ascDesc == "desc") 
        { 
         rows = (from j in jobs select j) 
         .OrderByDescending(j => j.ArrivalDate) 
        } 
        else 
        { 
         rows = (from j in jobs select j) 
         .OrderBy(j => j.ArrivalDate) 

        } 

        break; 
    } 

答えて

1

あり、それを行うためのより複雑な方法がありますが、私は特にLINQの初心者のために、読んでこの最も簡単見つける:

var first=true; 
foreach(DTOrder order in param.Order) 
{ 
    switch(order.Column) 
    { 
    case 0: 
     if (first) 
     { 
     rows=(order.Dir=="asc")?rows.OrderBy(r=>r.Id):rows.OrderByDescending(r=>r.Id); 
     } else { 
     rows=(order.Dir=="asc")?rows.ThenBy(r=>r.Id):rows.ThenByDescending(r=>r.Id); 
     } 
     break; 
    case 1: 
     ... 
    } 
    first=false; 
} 

通常しかし、あなたが後にTakeSkipをしたいと思います次のようなことをするでしょう:

public JsonResult IndexData(DTParameterViewModel param) 
{ 
    // Start with the base query 
    var rows = jobs.AsQueryable(); 

    // Order the query 
    var first=true; 
    foreach(DTOrder order in param.Order) 
    { 
    switch(order.Column) 
    { 
     case 0: 
     if (first) 
     { 
      rows=(order.Dir=="asc")?rows.OrderBy(r=>r.Id):rows.OrderByDescending(r=>r.Id); 
     } else { 
      rows=(order.Dir=="asc")?rows.ThenBy(r=>r.Id):rows.ThenByDescending(r=>r.Id); 
     } 
     break; 
     case 1: 
     ... 
    } 
    first=false; 
    } 

    // Partition the query 
    rows=rows 
    .Skip(param.Start) 
    .Take(param.Length) 
    .AsEnumerable(); // Or place the AsEnumerable in the projection 

    // Project the query 
    var result=rows.Select(j => new DataTableRowViewModel 
    { 
     Id = j.Id, 
     ArrivalDate = j.ArrivalDate.ToString(Constants.DATE_FORMAT), 
     DueDate = j.DueDate?.ToString(Constants.DATE_TIME_FORMAT), 
     Contact = j.Contact, 
     Priority = j.Priority.GetDisplayName(), 
     JobType = j.JobType.GetDisplayName(), 
     SapDate = j.SapDate.ToString() 
    }); 

    return result; 
} 

さらに最適化する(ジョブはデータベースから来ると仮定します)、intermを実行する必要がありますediate投影が変化する、戻ってくるの列を制限する:これに

// Partition the query 
    rows=rows 
    .Skip(param.Start) 
    .Take(param.Length) 
    .AsEnumerable(); // Or place the AsEnumerable in the projection 

    // Project the query 
    var result=rows.Select(j => new DataTableRowViewModel 

// Partition the query 
    var result1=rows 
    .Skip(param.Start) 
    .Take(param.Length) 
    /* Selecting only the fields we need */ 
    .Select(j=> new { 
     j.Id, 
     j.ArrivalDate, 
     j.DueDate, 
     j.Contact, 
     j.Priority, 
     j.JobType, 
     j.SapDate 
    }) 
    .AsEnumerable(); // Or place the AsEnumerable in the projection 

    // Project the query 
    var result=result1.Select(j => new DataTableRowViewModel 
+0

これはそんなに私を助け、みんなありがとう! – codingNightmares

関連する問題