LINQ to SQL:使用软删除过滤嵌套对象

我在我的数据库中使用软删除(
IsDeleted
字段)。我正在积极使用ѭ1​​ѭ和
AssociateWith
方法来检索和过滤嵌套记录。 事情是
AssociateWith
只适用于代表一对多关系的属性。
DataLoadOptions loadOptions = new DataLoadOptions();
loadOption.LoadWith<User>(u = > u.Roles);
loadOption.AssociateWith<User>(u = > u.Roles.Where(r = > !r.IsDeleted));
在上面的示例中,我只想说:我想检索具有相关(未删除)角色的用户。 但是当我有一对一的关系,例如
Document
- >
File
(唯一一个文件与文档相关)我无法过滤软删除的对象:
DataLoadOptions loadOptions = new DataLoadOptions();
loadOption.LoadWith<Document>(d = > d.File);
// the next certainly won't work
loadOption.AssociateWith<File>(f = > !f.IsDeleted);
那么,有没有想过如何在一对一的关系中过滤记录? 谢谢!     
已邀请:
到目前为止,我发现只有一个合适的解决方案(除了根本不使用软删除):删除实体更新的软删除关系。 例如。当我决定从文档中删除文件时,我执行以下操作:
// 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();
因此,这可能是对复杂数据检索的一对一关系过滤的替代。     
也许试试:
loadOptions.AssociateWith<File>(f => f.IsDeleted ? null : f);
这将为您提供null而不是IsDeleted为true的文件。     

要回复问题请先登录注册