Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为子对象包含导航属性_C#_Entity Framework 4.1_Repository Pattern - Fatal编程技术网

C# 如何为子对象包含导航属性

C# 如何为子对象包含导航属性,c#,entity-framework-4.1,repository-pattern,C#,Entity Framework 4.1,Repository Pattern,假设我的对象图如下所示: User => Friends => Clicks => Urls 因此,当我加载一个用户时,我还想快速加载导航属性Friends。我希望friend对象能够快速加载点击等等 使用我现在拥有的代码,我只能对1个级别执行此操作: public User GetUserById(int userId) { return Get(x => x.Id == userId, includeProperties: "Friends")

假设我的对象图如下所示:

User
 => Friends
   => Clicks
     => Urls
因此,当我加载一个用户时,我还想快速加载导航属性Friends。我希望friend对象能够快速加载点击等等

使用我现在拥有的代码,我只能对1个级别执行此操作:

public User GetUserById(int userId)
{
  return Get(x => x.Id == userId, includeProperties: "Friends").FirstOrDeafult();
}
这可能吗

  • 我正在使用MVC4.1
  • 我目前正在使用基于的存储库模式

显然,此存储库实现只是用逗号(
includeproperty.Split(newchar[]{','}
)拆分
includeproperty
参数,然后调用

query = query.Include(includeProperty);
对于拆分结果数组中的每个元素。对于您的示例,您可以使用虚线路径,然后:

return Get(x => x.Id == userId, includeProperties: "Friends.Clicks.Urls")
    .FirstOrDefault();
它将加载从根
用户
实体到最后一个导航属性
URL
的路径上的所有实体

如果您在
User
-比如
Profile
-中有另一个导航属性,并且您也希望加载该属性,那么似乎支持此语法:

return Get(x => x.Id == userId, includeProperties: "Profile,Friends.Clicks.Urls")
    .FirstOrDefault();

在加载子集合时为其设置orderby如何?这可能吗?@user1361315:这不是直接可能的,只有一些解决方法: