Nested LINQ query to select 'previous' value in a list

c#, linq

Solution

It is easy as converting your current query into a `LINQ` query:

var result = table.Select(x =>
    new
    {
        Date = x.Date,
        PrevDate = table.Where(y => y.Date < x.Date)
                        .Select(y => y.Date)
                        .Max()
    });

Problem

I have a list of dates. I would like to query the list and return a list of pairs where the first item is a date and the second is the date which occurs just before the first date (in the list). I know this could easily be achieved by sorting the list and getting the respective dates by index, I am curious how this could be achieved in LINQ. I've done this in SQL with the following query: ``` SELECT Date, (SELECT MAX(Date) FROM Table AS t2 WHERE t2.Date < t1.Date) AS PrevDate FROM Table AS t1 ```

Original source