Accepted answer

Maybe you are looking for this.

Filtere where

  • x.Alert.ShowOldAlerts == true


  • x.Alert.ShowOldAlerts == false && Read == false


var results = _context.GeneratedAlerts
    .Include(a => a.Alert)
    .Where(x => x.Alert.ShowOldAlerts || !x.Read)
    .Select(a => new GeneratedAlertResponse
        Id = a.Id,
        Alert = _mapper.Map<AlertInfoResponse>(a.Alert),
        ReceivedDate = a.ReceivedDate,
        Completed = a.Completed

If you need the results separated, it's probably best to still keep the one query (one round trip to the database), however you can filter the results after the fact.

var alerts = results.Where(x => x.Alert.ShowOldAlerts);

var read = results.Axcept(alerts ); 


For the first case which retrieves generated alerts if Read is true and ShowOldAlerts is false. If I understood the question correctly, this should work.

var alerts = _context.GeneratedAlerts
    .Where(c => c.Read && !c.Alert.ShowOldAlerts);

For the second case where you only retrieve the generated alerts only if ShowOldAlerts is false.

var alerts = _context.GeneratedAlerts
         .Where(c => c.Alert.ShowOldAlerts);

To return as one list, the conditions seem awkward, but here it is

var alerts = _context.GeneratedAlerts
    .Where(c => (c.Read && !c.Alert.ShowOldAlerts) || c.Alert.ShowOldAlerts);

Related Articles