Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修改了OrderBy的扩展以同时处理ThenBy_C# - Fatal编程技术网

C# 修改了OrderBy的扩展以同时处理ThenBy

C# 修改了OrderBy的扩展以同时处理ThenBy,c#,C#,我有一个扩展,它处理OrderBy和一堆asc/desc,工作起来就像一个符咒: public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool descending) {

我有一个扩展,它处理
OrderBy
和一堆asc/desc,工作起来就像一个符咒:

public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey>
     (this IEnumerable<TSource> source,
      Func<TSource, TKey> keySelector,
      bool descending)
{
    return descending ? source.OrderByDescending(keySelector)
                      : source.OrderBy(keySelector);
}

public static IOrderedQueryable<TSource> OrderByWithDirection<TSource, TKey>
    (this IQueryable<TSource> source,
     Expression<Func<TSource, TKey>> keySelector,
     bool descending)
{
    return descending ? source.OrderByDescending(keySelector)
                      : source.OrderBy(keySelector);
}
我希望扩展能够处理如下语法:

bool primaryDescending = false;
bool secondaryDescending = true;
var sortedList = unSortedList
    .OrderByWithDirection(o => o.primary, primaryDescending)
    .ThenByWithDirection(o => o.secondary, secondaryDescending)
    .ToList();

如果您有任何关于如何执行此操作的提示,我们将不胜感激。

您只需定义
IOrderedQueryable。然后使用方向
调用
ThenBy\uuuuu
,而不是
OrderBy\uuuu

public static IOrderedEnumerable<TSource> ThenByWithDirection<TSource, TKey>
     (this IOrderedEnumerable<TSource> source,
      Func<TSource, TKey> keySelector,
      bool descending)
{
    return descending ? source.ThenByDescending(keySelector)
                      : source.ThenBy(keySelector);
}

public static IOrderedQueryable<TSource> ThenByWithDirection<TSource, TKey>
    (this IOrderedQueryable<TSource> source,
     Expression<Func<TSource, TKey>> keySelector,
     bool descending)
{
    return descending ? source.ThenByDescending(keySelector)
                      : source.ThenBy(keySelector);
}
public static IOrderedEnumerable then by with direction
(这是一个不可数的来源,
Func键选择器,
布尔(下降)
{
返回降序?源。然后按降序(键选择器)
:source.ThenBy(按键选择器);
}
公共静态iOrderedQueryThenByWithDirection
(此IOrderedQueryable源,
表达式键选择器,
布尔(下降)
{
返回降序?源。然后按降序(键选择器)
:source.ThenBy(按键选择器);
}

您尝试过什么吗?既然您的
OrderBy
只返回一个
iorderenumerable
为什么不能为接受
iorderenumerable
ThenBy
创建完全相同的函数呢?已经有了.ThenBy()的Linq函数。还有用于OrderyByDescending()和ByDescending()的函数,因此无需构建您要执行的操作。@chris crush代码标准Linq扩展方法不允许您将方向指定为参数。正如Dai所说,此扩展的原因是能够传递参数并动态排序列表。当然,我可以用现有的方法来制作大量的IF汤,但是。。。让我们不要那样做。:)哦,那太令人沮丧了,我试过了,但当我从源代码处出来时,我只能选择OrderBy。这意味着我必须先下订单,然后再下订单。@Jippie正确
ThenBy
只能在
OrderBy
之后使用。查看返回类型:
IOrderedQueryable
而不是
IQueryable
。太棒了!非常感谢您的回答和解释!
public static IOrderedEnumerable<TSource> ThenByWithDirection<TSource, TKey>
     (this IOrderedEnumerable<TSource> source,
      Func<TSource, TKey> keySelector,
      bool descending)
{
    return descending ? source.ThenByDescending(keySelector)
                      : source.ThenBy(keySelector);
}

public static IOrderedQueryable<TSource> ThenByWithDirection<TSource, TKey>
    (this IOrderedQueryable<TSource> source,
     Expression<Func<TSource, TKey>> keySelector,
     bool descending)
{
    return descending ? source.ThenByDescending(keySelector)
                      : source.ThenBy(keySelector);
}