相关疑难解决方法(0)

如何仅包含相关实体的选定属性

我只能包括相关实体。

using (var context = new BloggingContext()) 
{ 
    // Load all blogs, all related posts
    var blogs1 = context.Blogs 
                       .Include(b => b.Posts) 
                       .ToList(); 
}
Run Code Online (Sandbox Code Playgroud)

但是,我不需要整个 BlogPost 实体。我只对特定的属性感兴趣,例如:

using (var context = new BloggingContext()) 
{ 
    // Load all blogs, all and titles of related posts
    var blogs2 = context.Blogs 
                       .Include(b => b.Posts.Select(p => p.Title) //throws runtime exeption
                       .ToList(); 

    foreach(var blogPost in blogs2.SelectMany(b => b.Posts))
    {
        Console.Writeline(blogPost.Blog.Id); //I need the object graph
        Console.WriteLine(blogPost.Title); //writes title
        Console.WriteLine(blogPost.Content); //writes null
    }
}
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core

7
推荐指数
3
解决办法
5593
查看次数

标签 统计

c# ×1

entity-framework-core ×1