Linq 和排序依据

发布于 2024-09-27 09:07:59 字数 682 浏览 2 评论 0原文

我有一个通用类,它可以使用通用 OrderBy 参数,

该类如下所示。

 class abc<T> where T : myType
 {
   public abc(....., orderBy_Argument ){ ... }
   void someMethod(arg1, arg2, bool afterSort = false)
   {
       IEnumerable<myType> res ;
       if ( afterSort && orderBy_Argument != null )
          res = src.Except(tgt).OrderBy( .... );
       else
          res = src.Except(tgt);
   }
}

orderBy 可以是各种类型,

例如

.OrderBy( person => person.FirstName )
.OrderBy( person => person.LastName )
.OrderBy( person => person.LastName, caseInsensitive etc )

目标是使 orderBy 成为一个论点,而不是在

任何想法中烘焙它?

I have a generic class which could use a generic OrderBy argument

the class is as follows

 class abc<T> where T : myType
 {
   public abc(....., orderBy_Argument ){ ... }
   void someMethod(arg1, arg2, bool afterSort = false)
   {
       IEnumerable<myType> res ;
       if ( afterSort && orderBy_Argument != null )
          res = src.Except(tgt).OrderBy( .... );
       else
          res = src.Except(tgt);
   }
}

The orderBy could be of various types

e.g.

.OrderBy( person => person.FirstName )
.OrderBy( person => person.LastName )
.OrderBy( person => person.LastName, caseInsensitive etc )

The goal is to make the orderBy an argument rather than bake it in

any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无悔心 2024-10-04 09:07:59

不要将参数传递给OrderBy,传递转换IEnumerable(或IQueryable)的函数,如可能是)。

修改您的示例以执行以下操作会产生以下程序:

using System;
using System.Collections.Generic;
using System.Linq;

class abc<T>  {
    Func<IEnumerable<T>,IEnumerable<T>> sorter;
    public abc(Func<IEnumerable<T>,IEnumerable<T>> sorter) {
        this.sorter=sorter ?? (x=>x);
    }
    public void someMethod(IEnumerable<T> src, bool afterSort = false) {
        var res= (afterSort?sorter:x=>x) (src.Skip(5).Take(10));

        Console.WriteLine(string.Join(", ",res.Select(el=>el.ToString()).ToArray()));
    }
}

public class Program {
    static void Main()    {
        var strs = Enumerable.Range(0,1000).Select(i=>i.ToString());

        var myAbc = new abc<string>(
            xs=>xs.OrderByDescending(x=>x.Length).ThenByDescending(x=>x.Substring(1))
        );

        myAbc.someMethod(strs);      //5, 6, 7, 8, 9, 10, 11, 12, 13, 14
        myAbc.someMethod(strs,true); //14, 13, 12, 11, 10, 5, 6, 7, 8, 9
    }
}

当然,这是一个非常奇怪的排序和无意义的 someMethod - 但它表明您可以传入一个非常灵活的排序委托(实际上,委托可以做的不仅仅是排序),只需很短的实现。

Don't pass in the arguments to OrderBy pass in a function which transforms an IEnumerable (or IQueryable, as it may be).

Modifying your example to do so results in the following program:

using System;
using System.Collections.Generic;
using System.Linq;

class abc<T>  {
    Func<IEnumerable<T>,IEnumerable<T>> sorter;
    public abc(Func<IEnumerable<T>,IEnumerable<T>> sorter) {
        this.sorter=sorter ?? (x=>x);
    }
    public void someMethod(IEnumerable<T> src, bool afterSort = false) {
        var res= (afterSort?sorter:x=>x) (src.Skip(5).Take(10));

        Console.WriteLine(string.Join(", ",res.Select(el=>el.ToString()).ToArray()));
    }
}

public class Program {
    static void Main()    {
        var strs = Enumerable.Range(0,1000).Select(i=>i.ToString());

        var myAbc = new abc<string>(
            xs=>xs.OrderByDescending(x=>x.Length).ThenByDescending(x=>x.Substring(1))
        );

        myAbc.someMethod(strs);      //5, 6, 7, 8, 9, 10, 11, 12, 13, 14
        myAbc.someMethod(strs,true); //14, 13, 12, 11, 10, 5, 6, 7, 8, 9
    }
}

Of course, this is a pretty weird ordering and a nonsensical someMethod - but it demonstrates that you can pass in a very flexible sorting delegate (indeed, the delegate could do more than just sort) with only a very short implementation.

恋你朝朝暮暮 2024-10-04 09:07:59

只需将排序键作为委托传递即可:

 class abc<T, TSort> where T : myType
 {
   public abc(....., Func<T, TSort> sortKeySelector ){ ... }
   void someMethod(arg1, arg2, bool afterSort = false)
   {
       IEnumerable<myType> res ;
       if ( afterSort && sortKeySelector != null )
          res = src.Except(tgt).OrderBy(sortKeySelector);
       else
          res = src.Except(tgt);
   }
}

缺点是它需要额外的类型参数......

Just pass the sort key as a delegate:

 class abc<T, TSort> where T : myType
 {
   public abc(....., Func<T, TSort> sortKeySelector ){ ... }
   void someMethod(arg1, arg2, bool afterSort = false)
   {
       IEnumerable<myType> res ;
       if ( afterSort && sortKeySelector != null )
          res = src.Except(tgt).OrderBy(sortKeySelector);
       else
          res = src.Except(tgt);
   }
}

The drawback is that it requires an extra type parameter...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文