实体框架4 - 如何从另一个表(通过外键连接)中的子元素数量上限 [英] Entity Framework 4 - How to cap the number of child elements from another table (connected via a foreign key)

查看:134
本文介绍了实体框架4 - 如何从另一个表(通过外键连接)中的子元素数量上限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据模型 Blog Post 。 BlogId是发布

I have two data models Blog and Post. BlogId is a foreign key on the Post table

public class Blog
{
   public int ID { get; set; }

   public string Title { get; set; }

   public virtual ICollection<Post> Posts { get; set; }

  ...  
}


public class Post
{
   public int ID { get; set; }

   public virtual int BlogId { get; set; }

   public string Title { get; set; }

  ...  
}

我的存储库很高兴,并从DB中提取所有内容。

Now this works fine and my Repository is happy and pulls everything as expected from DB.

我的问题是 - 是否有办法限制获得检索的 帖子 的数量。也许一些LINQ魔法?

My question is - Is there a way to limit the number of Posts that get retrieved. Perhaps some LINQ magic?

这是我当前在仓库中的方法:

Here is what my current method in the repository looks like:

public Business FindBlog(int id)
{
    return this.context.Get<Blog>().SingleOrDefault(x => x.ID == id);
}


推荐答案

Unfortunatelly EFv4不提供简单的方法来限制导航属性返回记录的数量。

Unfortunatelly EFv4 doesn't offer easy way to limit number of returned record for navigation property.

如果您使用 EntityObject 派生实体,您可以使用以下内容:

If you are using EntityObject derived entities you can use something like:

var blog = context.Blogs
                  .Single(b => b.Id == blogId);

var posts = blog.Posts
                .CreateSourceQuery()
                .OrderByDescending(p => p.Date)
                .Take(numberOfRecords)
                .ToList();

如果您使用的是POCO,则必须执行单独的查询(如果代理的POCO可以< a href =https://stackoverflow.com/questions/6658459/efficient-way-of-checking-if-many-to-many-relationship-exists-in-ef4-1/6661380#6661380>转换导航属性到 EntityCollection< Post> 以访问 CreateSourceQuery ):

If you are using POCOs you must execute separate query for that (in case of proxied POCOs you can convert navigation property to EntityCollection<Post> to get access to CreateSourceQuery):

var blog = context.Blogs
                  .Single(b => b.Id == blogId);

var posts = context.Posts
                   .Where(p => p.BlogId == blogId)
                   .OrderByDescending(p => p.Date)
                   .Take(numberOfPosts)
                   .ToList();

EFv4.1和DbContext API提供了仅加载有限数量的相关实体的方法:

EFv4.1 and DbContext API offers the way to load only limited number of related entities:

var blog = context.Blogs
                  .Single(b => b.Id == blogId);

context.Entry(blog)
       .Collection(b => b.Posts)
       .Query()
       .OrderByDescending(p => p.Date)
       .Take(numberOfPosts)
       .Load();

编辑:

你可以做到在具有投影的单个查询中:

You can do it in single query with projection:

var blog = context.Blogs
                  .Where(b => b.Id == blogId)
                  .Select(b => new 
                      {  
                          Blog = b,
                          Posts = b.Posts
                                   .OrderByDescending(p => Date)
                                   .Take(numberOfRecords)
                      })
                  .SingleOrDefault()

请注意,您必须访问匿名类型的第二张表格的帖子。

Just be aware that you must access posts from second paremeter of anonymous type.

这篇关于实体框架4 - 如何从另一个表(通过外键连接)中的子元素数量上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆