列表OrderBy在非基本类型的情况下不起作用

时间:2016-09-27 10:07:58

标签: c# linq list lambda

public class Sales : Resource
{
    public Channel ChannelName { get; set; }
    public string Type { get; set; }
}

public class Channel:Resource
{
    public string Code { get; set; }
}

列表排序适用于类型的原始(字符串)属性,但不适用于像Channel这样的非原始属性。有什么建议吗?

List<Sales> result = GetListofSales(); // list of sales

result = result.OrderBy(x =>x.Type).ThenBy(**y => y.ChannelName.code**).ToList();

1 个答案:

答案 0 :(得分:2)

实际上,我尝试了它并在下面的示例数据中对数据进行了很好的排序

列表列表=新列表();

        Sales sales = new Sales();
        sales.ChannelName = new Channel { Code = "B" };
        sales.Type = "C";
        list.Add(sales);

        sales = new Sales();
        sales.ChannelName = new Channel { Code = "AB" };
        sales.Type = "C";
        list.Add(sales);

        sales = new Sales();
        sales.ChannelName = new Channel { Code = "A" };
        sales.Type = "A";
        list.Add(sales);

        sales = new Sales();
        sales.ChannelName = new Channel { Code = "C" };
        sales.Type = "B";
        list.Add(sales);

        sales = new Sales();
        sales.ChannelName = new Channel { Code = "D" };
        sales.Type = "D";
        list.Add(sales);

结果将是 enter image description here

所以,正如你所看到的,它订购了A,然后是B,因为有2个销售,Type = C,它根据渠道代码对它们进行排序,尽管首先添加了通道B,然后是通道AB,结果显示通道AB然后B表示已经应用了二级订单