简体   繁体   中英

Sorting a list based on a condition placed on property of its inner list

I have a List<Order> and each Order has an inner list List<Item> , each Item has a property IsLateForDelivery . I want to sort the list of orders so that an Order with most number of Items that are Late for delivery is at index 0 .

Using System.Linq;
//List<Order> Orders
Orders.OrderByDescending(order => order.Items.Count(item => item.IsLateForDelivery))

This is how to do it:

class Program
{
    static void Main(string[] args)
    {
        List<Order> orders = new List<Order>();
        //List seed
        orders = orders.OrderByDescending(x => x.Items.Count(y => y.IsLateForDelivery)).ToList();
    }
}

public class Order
{
    public List<Item> Items { get; set; }
}

public class Item
{
    public bool IsLateForDelivery { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM