LINQ multiple order by

后端 未结 3 832
既然无缘
既然无缘 2020-12-21 05:09

I have created a function that has the follwing parameter:

List>> orderBy = null

T

相关标签:
3条回答
  •         foreach (Expression<Func<CatalogProduct, bool>> func in orderBy)
            {
                catalogProducts = catalogProducts.OrderBy(func);
            }
    

    This will be OK.

    0 讨论(0)
  • 2020-12-21 05:26

    try this

       IOrderedQueryable temp = null; 
       foreach (Expression<Func<CatalogProduct, bool>> func in orderBy) 
        { 
          if (temp == null) 
            { 
              temp = catalogProducts.OrderBy(func);
            } 
            else
            { 
              temp = temp.OrderBy(func); 
            } 
         }
    
    0 讨论(0)
  • 2020-12-21 05:36

    Two problems; firstly, ThanBy should be ThenBy; secondly, ThenBy is only available on the generic type, IOrderedQueryable<T>.

    So change to:

            IOrderedQueryable<CatalogProduct> temp = null;
            foreach (Expression<Func<CatalogProduct, bool>> func in orderBy) {
                if (temp == null) {
                    temp = catalogProducts.OrderBy(func);
                } else {
                    temp = temp.ThenBy(func);
                }
            }
    

    and you should be sorted.

    0 讨论(0)
提交回复
热议问题