Reversing ObservableCollection

本文关键字:ObservableCollection Reversing | 更新日期: 2023-09-27 18:20:01

如何在c#中反转ObservableCollection?

collection = new ObservableCollection<type>(collection.Reverse());

对我不起作用。我在互联网上搜索,但出乎意料地没有找到。

Reversing ObservableCollection

Reverse()不返回任何内容,您不能直接使用Reverse函数,您可以这样做,

示例,

 ObservableCollection<User> users = new ObservableCollection<User>();
        for (int i = 0; i < 5; i++)
        {
            User uo = new User();
            uo.ID = i;
            uo.Name = i.ToString();
            users.Add(uo);

        }
        List<User> reversed = getResult(users);
        ObservableCollection<User> result = new ObservableCollection<User>(reversed);
    }
    private List<User> getResult(ObservableCollection<User> res)
    {
        List<User> result = res.ToList();
        result.Reverse();
        return result;
    }
    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }