Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ到SQL:使用软删除筛选嵌套对象_C#_.net_Linq To Sql_Filtering_Soft Delete - Fatal编程技术网

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”上不受支持。