2010-05-21 83 views
1

我在我的數據庫中使用軟刪除(IsDeleted字段)。我正在積極使用LoadWithAssociateWith方法來檢索和過濾嵌套的記錄。LINQ to SQL:過濾軟刪除嵌套對象

事情是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); 

那麼,有沒有任何想法如何將一個一對一關係中篩選記錄?

謝謝!

回答

1

到目前爲止,我發現只有一個合適的解決方案(除了不使用軟刪除之外):刪除實體更新上的軟刪除關係。

E.g.當我決定從文件中刪除一個文件,我執行類似:

// 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(); 

所以,這可能是一到一個關係複雜的數據檢索篩選的替代品。

0

也許嘗試:

loadOptions.AssociateWith<File>(f => f.IsDeleted ? null : f); 

這會給你空文件,其中是請將isDeleted真正的來代替。

+1

不幸的是,這是行不通的。給出與'loadOption.AssociateWith (f =>!f.IsDeleted)'相同的錯誤:'子查詢不支持類型爲'Core.Entities.File'的'IsDeleted'。 – Alex 2010-05-21 15:25:39