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

Time:01-31

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();
}

CodePudding user response:

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();
}
  • Related