根据参数构建linq查询

时间:2016-07-27 16:11:43

标签: c# linq lambda

我尝试根据我的数据表中的参数创建查询。我试图执行类似下面的操作,除了我不断收到错误并且必须在每个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;
    }

1 个答案:

答案 0 :(得分: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;
}

进一步优化(假设作业来自数据库),那么你应该做一个中间投影来限制回来的列,改变:

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