簡體   English   中英

基於參數構建linq查詢

[英]build linq query based off parameters

我正在嘗試根據數據表中的參數創建查詢。 我一直在嘗試執行以下操作,除了不斷收到錯誤,並且必須在每個case語句中使用一個不同的參數(即基本上)重新創建整個linq查詢。 OrderBy或OrderByDecending。

有沒有一種方法可以基於參數構建查詢?

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

這樣做有更復雜的方法,但是我發現這是最容易閱讀的,尤其是對於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;
}

但是,通常情況下,您希望Skip訂單后執行“ 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;
}

進一步優化(假設作業來自數據庫),則應該進行中間投影以限制返回的列,並進行以下更改:

  // 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM