Selecting earliest date using linq/lambda

you can use OrderBy or OrderByDescending() to sort them on Date this way:

var list = techlinks.GetItems()
                    .Where(p => p.Status == 1)
                    .OrderBy(x=>x.Date).First(); // this will give oldest date

and:

var list = techlinks.GetItems()
                    .Where(p => p.Status == 1)
                    .OrderByDescending(x=>x.Date).First(); // this will give latest date

Here's another way.

var list=techlinks.GetItems()
                  .Where(p=> p.Status==1)
                    .Min(d => d.Date)
                      .Single();

If there might be multiple items all with the earliest date:

var list = techlinks.GetItems()
    .Where(p => p.Status == 1)
    .OrderBy(x=>x.Date)
    .GroupBy(x => x.Date)
    .First()
    .ToList()

Student student = _context.Set<Student>()
          .Where(p => p.StudentID == ID.Value)
          .OrderBy(p => p.AddedDate)
          .FirstOrDefault();

Tags:

C#

Linq

Lambda