我正在使用數據庫模式,其中記錄在更新時不會被覆蓋。而是添加記錄的新副本並將其標記爲「當前」。實體框架 - 急於使用過濾器加載?
例如:
Id | Current | Name | Owner
1 | false | Foo | Bar
1 | false | Foo | Bazz
1 | true | Foo | Buzz
在我的模型我有有許多與此相關的Post
個Blog
實體。每個Post
有許多與之相關的Comment
S:
public class Blog
{
public int Id {get; set};
public bool Current {get; set};
public ICollection<Post> Posts {get; set;}
}
public class Post
{
public int Id {get; set};
public bool Current {get; set};
public ICollection<Comment> Comments {get; set;}
}
public class Comment
{
public int Id {get; set};
public bool Current {get; set};
}
我想在this example from MSDN熱切加載Blog
與所有Post
S和所有的Comment
小號很像:
using (var context = new BloggingContext()) { // Load all blogs, all related posts, and all related comments var blogs1 = context.Blogs .Include(b => b.Posts.Select(p => p.Comments)) .ToList(); }
但是,我想只包含數據庫記錄Current == true
。我如何用LINQ到EF來做到這一點?理想情況下,條件將進入JOIN
的ON
條款 - 這可能嗎?
問題不是很清楚,大約.INCLUDE(B => b.Posts.Select(P => p.Comments.Where(註釋=> comment.Current)))什麼?? –
@ a-t謝謝。什麼不清楚?您的建議不會擴展到博客和帖子,並給我:「包含路徑表達式必須引用該類型上定義的導航屬性。對引用導航屬性使用虛線路徑,對集合導航屬性使用選擇運算符。 – urig