LINQ多个订单

Wou*_*ter 4 c# linq .net-4.0 sql-order-by

我创建了一个具有以下参数的函数:

List<Expression<Func<CatalogProduct, bool>>> orderBy = null
Run Code Online (Sandbox Code Playgroud)

这个参数是可选的,如果它被填充,它应该为我创建一个order by,而不是为我创建一个order,这样我就可以在SQL server上订购结果.

我试过了:

            IOrderedQueryable temp = null;
            foreach (Expression<Func<CatalogProduct, bool>> func in orderBy)
            {
                if (temp == null)
                {
                    temp = catalogProducts.OrderBy(func);
                }
                else
                {
                    temp = temp.ThanBy(func);
                }
            }
Run Code Online (Sandbox Code Playgroud)

但是比没有重新定义.有人知道我怎么能解决这个问题吗?


我将其更改为.ThenBy()但是只允许直接在.OrderBy()之后,而不是在IOrderedQueryable上

so temp = catalogProducts.OrderBy(func).ThenBy(func); 允许但是temp = catalogProducts.OrderBy(func); temp = temp.ThenBy(func); issn't

还有其他建议吗?

Mar*_*ell 10

两个问题; 首先,ThanBy应该是ThenBy; 其次,ThenBy仅适用于通用类型,IOrderedQueryable<T>.

所以改为:

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

你应该排序.