LINQ OrderBy versus ThenBy

前端 未结 4 523
长发绾君心
长发绾君心 2020-11-27 12:40

Can anyone explain what the difference is between:

tmp = invoices.InvoiceCollection
              .OrderBy(sort1 => sort1.InvoiceOwner.LastName)
                  


        
4条回答
  •  -上瘾入骨i
    2020-11-27 12:56

    I found this distinction annoying in trying to build queries in a generic manner, so I made a little helper to produce OrderBy/ThenBy in the proper order, for as many sorts as you like.

    public class EFSortHelper
    {
      public static EFSortHelper Create(IQueryable query)
      {
        return new EFSortHelper(query);
      }
    }  
    
    public class EFSortHelper : EFSortHelper
    {
      protected IQueryable unsorted;
      protected IOrderedQueryable sorted;
    
      public EFSortHelper(IQueryable unsorted)
      {
        this.unsorted = unsorted;
      }
    
      public void SortBy(Expression> sort, bool isDesc = false)
      {
        if (sorted == null)
        {
          sorted = isDesc ? unsorted.OrderByDescending(sort) : unsorted.OrderBy(sort);
          unsorted = null;
        }
        else
        {
          sorted = isDesc ? sorted.ThenByDescending(sort) : sorted.ThenBy(sort)
        }
      }
    
      public IOrderedQueryable Sorted
      {
        get
        {
          return sorted;
        }
      }
    }
    

    There are a lot of ways you might use this depending on your use case, but if you were for example passed a list of sort columns and directions as strings and bools, you could loop over them and use them in a switch like:

    var query = db.People.AsNoTracking();
    var sortHelper = EFSortHelper.Create(query);
    foreach(var sort in sorts)
    {
      switch(sort.ColumnName)
      {
        case "Id":
          sortHelper.SortBy(p => p.Id, sort.IsDesc);
          break;
        case "Name":
          sortHelper.SortBy(p => p.Name, sort.IsDesc);
          break;
          // etc
      }
    }
    
    var sortedQuery = sortHelper.Sorted;
    

    The result in sortedQuery is sorted in the desired order, instead of resorting over and over as the other answer here cautions.

提交回复
热议问题