LINQtoEntities语句告诉我是否存在特定的子实体?
下面是一个foreach语句,我想用Linq表示给实体。它在父实体(currentFactSheet)的子实体(附件)中循环,以查看是否存在具有特定文件名的附件如何将此过程代码压缩为Linq to Entites语句?LINQtoEntities语句告诉我是否存在特定的子实体?,linq,entity-framework,entity-framework-4,linq-to-entities,Linq,Entity Framework,Entity Framework 4,Linq To Entities,下面是一个foreach语句,我想用Linq表示给实体。它在父实体(currentFactSheet)的子实体(附件)中循环,以查看是否存在具有特定文件名的附件如何将此过程代码压缩为Linq to Entites语句? FactSheet currentFactSheet = mainWindow.GetCurrentFactSheet(); bool attachmentExists = false; foreach (var thisAttachment in currentFactShe
FactSheet currentFactSheet = mainWindow.GetCurrentFactSheet();
bool attachmentExists = false;
foreach (var thisAttachment in currentFactSheet.AttachmentsNav)
{
if (thisAttachment.FileName == nameOfAttachedFile)
{
attachmentExists = true;
}
}
这是一个局部图像,显示了FactSheet(左)和通过名为AttachmentsNav的导航属性关联的附件实体:
我想查询内存中的实体,以避免往返数据库。我发现只搜索父级实体。我做了很多尝试,但他们从来并没有在我的子实体(特别是Attachment.FileName)上用字段名显示intellisense
提前感谢。试试这个:
bool attachmentExists = currentFactSheet.AttachmentsNav.Any(a => a.FileName == nameOfAttachedFile);