Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Searching for an item in a list that's nested inside another list based on a property value in C# using LINQ?

Is there a way to search for an item in a list that’s nested inside another list based on a property value using LINQ?

Given the follow models below, for a given Order (variable customerOrder), I want to return the earliest order date (Date) where the Day is "Sunday".

models:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Order
{
    public int Id { get; set; }
    public List<OrderLine> OrderLines { get; set; }
}

public class OrderLine
{
    public string Description { get; set; }
    public List<OrderDate> OrderDates { get; set; }
}

public class OrderDate
{
    public DateTime Date { get; set; }
    public string Day { get; set; }
}

code:

var dates = new List<DateTime>();
foreach(var a in customerOrder.OrderLines)
{
    var orderDate = a.OrderDates.Where(x => x.DateTypeId.Equals("Sunday")).FirstOrDefault();
    dates.Add(orderDate.ActualDate);
}
dates.OrderBy(d => d.Date);

return dates.FirstOrDefault();

>Solution :

You can use Linq to achieve your result:

customerOrder.OrderLines
    .Select(ol => ol.OrderDates
        .Where(x => x.Day.Equals("Sunday"))
        .FirstOrDefault())
    .Where(d => d != null)
    .OrderBy(d => d.Date)
    .FirstOrDefault();

Here is a Linqpad query with some test data and dump for you to try.
Simply copy and paste in Linqpad.

void Main()
{
    var customerOrder = new Order
    {
        Id = 1,
        OrderLines = Enumerable
            .Range(0, 10)
            .Select(i => new OrderLine
            {
                Description = $"Line Description {i}",
                OrderDates = Enumerable.Range(0, 10)
                    .Select(j => new OrderDate
                    {
                        Date = DateTime.Now.AddDays(i+j),
                        Day = DateTime.Now.AddDays(i+j).DayOfWeek.ToString()
                    })
                    .ToList()
            })
            .ToList()
        }
        .Dump();

    customerOrder.OrderLines
            .Select(ol => ol.OrderDates
                .Where(x => x.Day.Equals("Sunday"))
                .FirstOrDefault())
            .Where(d => d != null)
            .OrderBy(d => d.Date)
            .Dump()
            .FirstOrDefault()
            .Dump();
}

// You can define other methods, fields, classes and namespaces here

public class Order
{
    public int Id { get; set; }
    public List<OrderLine> OrderLines { get; set; }
}

public class OrderLine
{
    public string Description { get; set; }
    public List<OrderDate> OrderDates { get; set; }
}

public class OrderDate
{
    public DateTime Date { get; set; }
    public string Day { get; set; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading