有条件订购

时间:2018-11-30 01:16:42

标签: c# linq lambda

尝试按以下顺序获取此商品,但不起作用,它不会返回正确的顺序,在“未付款”之前选择了“已取消”,请告诉我

       dt = dt.AsEnumerable()
            .OrderBy(x => x["TicketStatus"].ToString())
            .ThenBy(x => x["TicketStatus"].ToString() == "Attended")
            .ThenBy(x => x["TicketStatus"].ToString() == "Issued")
            .ThenBy(x => x["TicketStatus"].ToString() == "Unpaid")
            .ThenBy(x => x["TicketStatus"].ToString() == "Cancelled")
            .GroupBy(x => new {EventID = x["EventID"].ToString(), ContactID = x["ContactID"].ToString()})
            .Select(x => x.FirstOrDefault()).CopyToDataTable();

2 个答案:

答案 0 :(得分:1)

尝试类似的方法

string[] status= { "Attended", "Issued", "Unpaid", "Cancelled" };

var sortstatus = from s in status
                  orderby s
                  select s;

答案 1 :(得分:1)

只要找出这项工作

        List<string> sortOrder = new List<string> { "Attended", "Issued", "Unpaid", "Cancelled" };

        dt = dt.AsEnumerable()
            .OrderBy(x => sortOrder.IndexOf(x["TicketStatus"].ToString()))
            .GroupBy(x => new {EventID = x["EventID"].ToString(), ContactID = x["ContactID"].ToString()})
            .Select(x => x.FirstOrDefault()).CopyToDataTable();