Here are my DataModels:
public class GeneratedAlertDataModel : BaseEntity
{
/// <summary>
/// the alert that will be generated
/// from configuration of alert
/// </summary>
public string AlertText { get; set; }
public bool Read { get; set; }
public AlertDataModel Alert { get; set; }
public DateTime ReceivedDate { get; set; }
public bool Completed { get; set; }
}
public class AlertDataModel : BaseEntity
{
[Required]
[MaxLength(250)]
public string Designation { get; set; }
[Required]
public PriorityLevel PriorityLevel { get; set; }
public bool ShowOldAlerts { get; set; }
}
The relation between the two tables in many to one, an Alert have multiple generatedAlerts.
I have two cases:
- Is to retreive a list of generated alert where read = true, if the property ShowOldAlerts in alert is set to false.
- Is to retreive all the alerts if the property ShowOldAlerts in Alert is set to true. But I have to return the all this is one list.
I have tried doing this but it doesn't work:
var generatedAlertDbQuery = _context.GeneratedAlerts.Include(a => a.Alert)
// if set ShowAlert to false, get only the not viewed generated alerts
.Where(a => a.Alert.ShowOldAlerts).Select(a => a)
// if set ShowAlert to true, get only the not viewed generated alerts
.Where(a => !a.Alert.ShowOldAlerts).Select(a => a).Where(a => !a.Read)
.OrderBy(a => a.ReceivedDate) // Ordered by the receive date
.Take(take) // Taking maximum (will be configured in client)
.AsQueryable();
var generatedAlertResponse = await generatedAlertDbQuery.Select(a => new GeneratedAlertResponse
{
Id = a.Id,
Alert = _mapper.Map<AlertInfoResponse>(a.Alert),
ReceivedDate = a.ReceivedDate,
Completed = a.Completed,
}).ToListAsync();