Linq - “保存” OrderBy 操作 (c#)

发布于 2024-09-25 13:25:17 字数 200 浏览 5 评论 0原文

假设我有 C# 中某种类型的通用列表 L 。然后,使用 linq 对其调用 OrderBy(),并传入 lambda 表达式。

如果我随后重新分配L,则之前的订单操作显然会丢失。

在重新分配列表之前,有什么方法可以“保存”我在列表中使用的 lambda 表达式并重新应用它吗?

Assume I have generic list L of some type in c#. Then, using linq, call OrderBy() on it, passing in a lambda expression.

If I then re-assign the L, the previous order operation will obviously be lost.

Is there any way I can 'save' the lambda expression I used on the list before i reassigned it, and re-apply it?

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

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

发布评论

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

评论(2

不必了 2024-10-02 13:25:17

使用 Func delegate 存储您的订单,然后将其传递给 OrderBy 方法:

Func<int, int> orderFunc = i => i; // func for ordering
var list = Enumerable.Range(1,10).OrderByDescending(i => i); // 10, 9 ... 1
var newList = list.OrderBy(orderFunc); // 1, 2 ... 10

作为另一个示例,考虑一个 Person 类:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

现在您想要保留按 Name 属性排序的排序顺序。在这种情况下,FuncPerson 类型 (T) 进行操作,并且 TResult 将是一个字符串,因为 < code>Name 是一个字符串,也是您排序的依据。

Func<Person, string> nameOrder = p => p.Name;

var list = new List<Person>
{
    new Person { Id = 1, Name = "ABC" },
    new Person { Id = 2, Name = "DEF" },
    new Person { Id = 3, Name = "GHI" },
};

// descending order by name
foreach (var p in list.OrderByDescending(nameOrder))
    Console.WriteLine(p.Id + ":" + p.Name);

// 3:GHI
// 2:DEF
// 1:ABC

// re-assinging the list
list = new List<Person>
{
    new Person { Id = 23, Name = "Foo" },
    new Person { Id = 14, Name = "Buzz" },
    new Person { Id = 50, Name = "Bar" },
};

// reusing the order function (ascending by name in this case)
foreach (var p in list.OrderBy(nameOrder))
    Console.WriteLine(p.Id + ":" + p.Name);

// 50:Bar
// 14:Buzz
// 23:Foo

编辑:如果您需要 ListOrderBy 调用,请务必在 OrderBy 调用之后添加 ToList()。 T>,因为 LINQ 方法将返回 IEnumerable

Use a Func delegate to store your ordering then pass that to the OrderBy method:

Func<int, int> orderFunc = i => i; // func for ordering
var list = Enumerable.Range(1,10).OrderByDescending(i => i); // 10, 9 ... 1
var newList = list.OrderBy(orderFunc); // 1, 2 ... 10

As another example consider a Person class:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now you want to preserve a sort order that sorts by the Name property. In this case the Func operates on a Person type (T) and the TResult will be a string since Name is a string and is what you are sorting by.

Func<Person, string> nameOrder = p => p.Name;

var list = new List<Person>
{
    new Person { Id = 1, Name = "ABC" },
    new Person { Id = 2, Name = "DEF" },
    new Person { Id = 3, Name = "GHI" },
};

// descending order by name
foreach (var p in list.OrderByDescending(nameOrder))
    Console.WriteLine(p.Id + ":" + p.Name);

// 3:GHI
// 2:DEF
// 1:ABC

// re-assinging the list
list = new List<Person>
{
    new Person { Id = 23, Name = "Foo" },
    new Person { Id = 14, Name = "Buzz" },
    new Person { Id = 50, Name = "Bar" },
};

// reusing the order function (ascending by name in this case)
foreach (var p in list.OrderBy(nameOrder))
    Console.WriteLine(p.Id + ":" + p.Name);

// 50:Bar
// 14:Buzz
// 23:Foo

EDIT: be sure to add ToList() after the OrderBy calls if you need a List<T> since the LINQ methods will return an IEnumerable<T>.

锦上情书 2024-10-02 13:25:17

IEnumerable 上调用 ToList()ToArray() 将导致立即对其求值。然后,您可以分配结果列表或数组来“保存”您的有序列表。

Calling ToList() or ToArray() on your IEnumerable<T> will cause it to be immediately evaluated. You can then assign the resulting list or array to "save" your ordered list.

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