罗汉松驻扎的工作基地
2022-06-30 15:58
采纳率: 80%
浏览 32

请教一个C# Linq 排序的问题(自定义排序)

各路大深,请教一个C# Linq 排序的问题,

我有个实体类是Class : User
里面有个属性是


    public class User
    {
        public string UName { get; set; }
        public int UAge { get; set; }
        public string ItemType { get; set; }
    }
大概有四种字符类型:   Active ,  Working  , Plan , PlanSub  

如果我按照普通方法排序
 List<User> userList = new List<User>();
            User u1 = new User { UName = "Tom", UAge = 9, ItemType = "active" };
            User u2 = new User { UName = "mary", UAge = 9, ItemType = "working" };
            User u3 = new User { UName = "jack", UAge = 9, ItemType = "active" };
            User u4 = new User { UName = "lee", UAge = 9, ItemType = "Plan" };
            User u5 = new User { UName = "Hua", UAge = 9, ItemType = "active" };
            User u6 = new User { UName = "Joph", UAge = 9, ItemType = "PlanSub" };

            userList.OrderBy(x => x.ItemType);

那么得到的顺序是  Active ,  Plan , PlanSub  ,Working   
这个order是根据字母顺序来排列的,也就是最普通的排序。

不过,现在用户想要特别排序 : Working , Active ,  Plan , PlanSub  
字母W 排在最前面 , A排在中间 , P放在最后。

请问这种自定义排序,可以如何用linq实现
感谢帮忙

3条回答 默认 最新

相关推荐 更多相似问题