[英]Select query on sub table
我正在编写基本的CRM来捕获潜在客户。
您可以向销售线索添加注释。
我想返回最近两天内没有备注的潜在客户列表。
我无法解决linq查询以启用此功能。
到目前为止,我有以下内容将不带注释地返回所有线索。
vm.LeadsNoNotes = _context.Lead.Include(x => x.Notes).Where(x => x.Notes.Count == 0).Take(10).ToList();
下面是我的模型结构
public class Lead
{
public int LeadId { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Comment { get; set; }
public string Status { get; set; }
public string Source { get; set; }
public string PreferedContactMethod { get; set; }
public string Vehicle { get; set; }
public List<Note> Notes { get; set; }
}
public class Note
{
public int NoteId { get; set; }
public int? LeadId { get; set; }
public int? CreditApplicationId { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public DateTime NoteDate { get; set; }
public string UserId { get; set; }
[Required]
public string Subject { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
}
假设Note
类具有类似DateCreated
属性的内容,则您需要以下线索:
这直接转化为以下情况:
var twoDaysAgo = DateTime.Now.AddDays(-2);
.Where(
x =>
x.Notes.Count == 0
||
x.Notes.All(note => note.DateCreated.Date < twoDaysAgo.Date);
)
注意.Date
确保只比较日期。 如果还应该考虑时间,请删除。
而且,即使没有条件的第一部分,该方法也可以工作,因为对于空的queryables , All返回true ,但这可能有点不直观。
vm.LeadsNoNotes = _context.Lead.Include(x => x.Notes).Where(x => x.Notes.Count == 0 && x.Notes.DateCreatd > DateTime.Now().AddDays(-2)).Take(10).ToList();
我在您的where子句中包含了一个日期比较条件。 我尚未测试以上代码,但您可以尝试一下。 它会给你一个想法。 您可以修改日期部分以获得准确的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.