0

這是我希望在Linq to Entities中表達的一條foreach語句。它通過父實體(currentFactSheet)的子實體(附件)循環查看附件是否與特定的FileName存在。 如何將此過程代碼壓縮到Linq到Entites語句中?Linq to Entities聲明告訴我一個特定的子實體是否存在?

FactSheet currentFactSheet = mainWindow.GetCurrentFactSheet(); 

bool attachmentExists = false; 
foreach (var thisAttachment in currentFactSheet.AttachmentsNav) 
{ 
    if (thisAttachment.FileName == nameOfAttachedFile) 
    { 
     attachmentExists = true; 
    } 
} 

是表示簡報(左)和經由導航屬性命名AttachmentsNav相關聯的附件實體的部分圖像:

enter image description here

我想在存儲器實體查詢,以避免往返數據庫。我發現examples like this只搜索父級別。我做了很多嘗試,但是他們從來沒有在我的子實體(特別是Attachment.FileName)上帶出字段名稱的智能感知。

在此先感謝。

回答

2

試試這個:

bool attachmentExists = currentFactSheet.AttachmentsNav.Any(a => a.FileName == nameOfAttachedFile); 
+0

完美的作品。謝謝! – DeveloperDan