C# LINQ到SQL:使用软删除筛选嵌套对象
我正在数据库中使用软删除(C# LINQ到SQL:使用软删除筛选嵌套对象,c#,.net,linq-to-sql,filtering,soft-delete,C#,.net,Linq To Sql,Filtering,Soft Delete,我正在数据库中使用软删除(IsDeleted字段)。我正在积极使用LoadWith和AssociateWith方法来检索和过滤嵌套记录 问题是,AssociateWith只适用于表示一对多关系的属性 DataLoadOptions loadOptions = new DataLoadOptions(); loadOption.LoadWith<User>(u = > u.Roles); loadOption.AssociateWith<User>(u = >
IsDeleted
字段)。我正在积极使用LoadWith
和AssociateWith
方法来检索和过滤嵌套记录
问题是,AssociateWith
只适用于表示一对多关系的属性
DataLoadOptions loadOptions = new DataLoadOptions();
loadOption.LoadWith<User>(u = > u.Roles);
loadOption.AssociateWith<User>(u = > u.Roles.Where(r = > !r.IsDeleted));
那么,有没有办法在一对一的关系中过滤记录
谢谢 不妨试试:
loadOptions.AssociateWith<File>(f => f.IsDeleted ? null : f);
loadOptions.AssociateWith(f=>f.IsDeleted?null:f);
这将为您提供null,而不是IsDeleted为true的文件。请尝试:
loadOptions.AssociateWith<File>(f => f.IsDeleted ? null : f);
loadOptions.AssociateWith(f=>f.IsDeleted?null:f);
这将为您提供null,而不是IsDeleted为true的文件。到目前为止,我只找到了一个适合的解决方案(除了完全不使用软删除之外):在实体更新时删除软删除关系 例如,当我决定从文档中删除文件时,我会执行以下操作:
// this may be a part of update method
var file = document.File;
if (file.IsDeleted)
{
// attach soft deleted file
context.Files.Attach(file, true);
// remove a file reference
document.File = null;
}
// attach document entity and submit changes
context.Documents.Attach(document, true);
context.SubmitChanges();
因此,这可能取代了复杂数据检索中的一对一关系过滤。到目前为止,我只找到了一个适合的解决方案(除了完全不使用软删除之外):在实体更新时删除软删除关系 例如,当我决定从文档中删除文件时,我会执行以下操作:
// this may be a part of update method
var file = document.File;
if (file.IsDeleted)
{
// attach soft deleted file
context.Files.Attach(file, true);
// remove a file reference
document.File = null;
}
// attach document entity and submit changes
context.Documents.Attach(document, true);
context.SubmitChanges();
因此,这可能取代了复杂数据检索中的一对一关系过滤。不幸的是,这不起作用。给出与
loadOption.AssociateWith(f=>!f.IsDeleted)
相同的错误:子查询在“Core.Entities.File”类型的“IsDeleted”上不受支持。
很遗憾,这不起作用。与loadOption.AssociateWith(f=>!f.IsDeleted)
给出的错误相同:子查询在类型为“Core.Entities.File”的“IsDeleted”上不受支持。