在 include 里面过滤一个 属性 列表
Filter a property List inside in include
我需要你的帮助
我尝试用 .Include 创建一个 linq 语句,但我的问题是我在 mi class 中有一个 属性 是一个列表,它是我的 class 具体来说:
public partial class document
{
public int ID { get; set; }
public string Amount { get; set; }
public List<Log> Log { get; set; }
}
这是class日志
public partial class Log
{
[Key]
public int ID { get; set; }
[Required]
public Status Status { get; set; }
[Column(TypeName = "text")]
public string Description { get; set; }
public DateTime? DateLog { get; set; }
public int? DocumentID{ get; set; }
[ForeignKey("DocumentID")]
public Document Document{ get; set; }
}
我的问题是我不知道如何过滤文档中的列表记录以包含在 class 中,我需要获取整个文档 class 并过滤日志只显示status = recieved,一个文件可以有很多logs
我试过但没成功
var Result = db.document
.Include(m => m.Log.Where(c => c.Status == Status.Recieved));
我收到下一个错误
"the include path expression must refer to a navigation property defined on the type. use dotted paths for reference navigation properties and the select operator for collection navigation properties.\r\nparameter name: path"
感谢您的帮助
Include
用于包含与实体的关系并获取相关的实体属性,请查看文档 - Fetching related data
如果您 select 文档没有 Include
这样的
var documents = await db.document.ToListAsync();
您获取文档数据,其中 Log
将为空。
你需要这样的东西:
var result = await db.document
.Select(w=> new
{
document = w,
log = w.Log.Where(c => c.Status == Status.Recieved).ToList()
}).ToListAsync();
EF 确实支持一些自动过滤规则,以帮助理解软删除 (IsActive) 和多租户 (ClientId) 等概念,但并不真正适用于您想要应用“已接收”等情境过滤器的场景" 文件。
EF 实体应被视为反映数据状态的模型。要过滤这样的结果更多的是您可以通过投影实现的视图模型状态:
var result = db.document.Select(d => new DocumentViewModel
{
DocumentId = d.DocumentId,
// .. fill in other required details...
ReceivedLogs = d.Logs
.Where(l => l.Status == Status.Received)
.Select(l => new LogViewModel
{
// Fill needed log details...
}).ToList()
}).ToList();
否则,如果您正在对实体进行本地操作并且只需要文档和接收到的日志条目:
var documentDetails = db.document
.Where(d => d.DocumentId == documentId)
.Select(d => new
{
Document = d,
ReceivedLogs = d.Logs
.Where(l => l.Status == Status.Received)
.ToList()
}).Single();
documentDetails.Document.Logs
不会被预加载,如果你访问它会触发延迟加载,但documentDetails
确实包含相关的Received日志可以访问。作为匿名类型不适合返回,只能在本地消费。
我需要你的帮助
我尝试用 .Include 创建一个 linq 语句,但我的问题是我在 mi class 中有一个 属性 是一个列表,它是我的 class 具体来说:
public partial class document
{
public int ID { get; set; }
public string Amount { get; set; }
public List<Log> Log { get; set; }
}
这是class日志
public partial class Log
{
[Key]
public int ID { get; set; }
[Required]
public Status Status { get; set; }
[Column(TypeName = "text")]
public string Description { get; set; }
public DateTime? DateLog { get; set; }
public int? DocumentID{ get; set; }
[ForeignKey("DocumentID")]
public Document Document{ get; set; }
}
我的问题是我不知道如何过滤文档中的列表记录以包含在 class 中,我需要获取整个文档 class 并过滤日志只显示status = recieved,一个文件可以有很多logs
我试过但没成功
var Result = db.document
.Include(m => m.Log.Where(c => c.Status == Status.Recieved));
我收到下一个错误
"the include path expression must refer to a navigation property defined on the type. use dotted paths for reference navigation properties and the select operator for collection navigation properties.\r\nparameter name: path"
感谢您的帮助
Include
用于包含与实体的关系并获取相关的实体属性,请查看文档 - Fetching related data
如果您 select 文档没有 Include
这样的
var documents = await db.document.ToListAsync();
您获取文档数据,其中 Log
将为空。
你需要这样的东西:
var result = await db.document
.Select(w=> new
{
document = w,
log = w.Log.Where(c => c.Status == Status.Recieved).ToList()
}).ToListAsync();
EF 确实支持一些自动过滤规则,以帮助理解软删除 (IsActive) 和多租户 (ClientId) 等概念,但并不真正适用于您想要应用“已接收”等情境过滤器的场景" 文件。
EF 实体应被视为反映数据状态的模型。要过滤这样的结果更多的是您可以通过投影实现的视图模型状态:
var result = db.document.Select(d => new DocumentViewModel
{
DocumentId = d.DocumentId,
// .. fill in other required details...
ReceivedLogs = d.Logs
.Where(l => l.Status == Status.Received)
.Select(l => new LogViewModel
{
// Fill needed log details...
}).ToList()
}).ToList();
否则,如果您正在对实体进行本地操作并且只需要文档和接收到的日志条目:
var documentDetails = db.document
.Where(d => d.DocumentId == documentId)
.Select(d => new
{
Document = d,
ReceivedLogs = d.Logs
.Where(l => l.Status == Status.Received)
.ToList()
}).Single();
documentDetails.Document.Logs
不会被预加载,如果你访问它会触发延迟加载,但documentDetails
确实包含相关的Received日志可以访问。作为匿名类型不适合返回,只能在本地消费。