【发布时间】:2021-02-05 23:23:23
【问题描述】:
这是我的数据模型:
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; }
}
两个表之间的关系是多对一的,一个Alert有多个生成的Alerts。
我有两种情况:
- 如果警报中的属性 ShowOldAlerts 设置为 false,则检索生成的警报列表,其中 read = true。
- 如果警报中的属性 ShowOldAlerts 设置为 true,则检索所有警报。 但我必须返回所有这些都是一个列表。
我尝试过这样做,但它不起作用:
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();
【问题讨论】:
-
也许我不明白这个问题......两个实体有什么关系?你有一个
Alert对象列表;有些人可能拥有真实的财产;其他为假。这些值如何影响您选择all 还是some 生成的警报? -
关系是多对一的,alerts有多个generateAlerts,所以alert类基本上就是一个配置,产生多个alerts
标签: c# linq asp.net-core