Search code examples
linqentity-framework-6.net-6.0

ASP.NET Core : getting values ​if its id exists in relational table with EF6 or Linq?


I have a date table and a time table. These two tables are related. In the date table, I created the date and defined times for this date. I want to get the list of dates for which the time is specified from the database.

This is my query:

public async Task<List<ReserveDate>> GetReservedates()
{
    return await _context.ReserveDates
        .Include(t => t.ReserveTimes)
        .OrderBy(r => r.DateReserve)
        .ToListAsync();
}

Solution

  • If I understand question correctly, you need filter by existence of ReserveTimes:

    public async Task<List<ReserveDate>> GetReservedates()
    {
        return await _context.ReserveDates
            .Include(t => t.ReserveTimes)
            .Where(t => t.ReserveTimes.Any())
            .OrderBy(r => r.DateReserve)
            .ToListAsync();
    }