Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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#_Asp.net_Asp.net Web Api_Linq To Entities - Fatal编程技术网

C# 查询具有最低级别筛选器的多级实体

C# 查询具有最低级别筛选器的多级实体,c#,asp.net,asp.net-web-api,linq-to-entities,C#,Asp.net,Asp.net Web Api,Linq To Entities,所以我有3个实体类: public partial class Event { public Event() { Recurrences = new HashSet<Recurrence>(); } public int Id { get; set; } public ICollection<Recurrence> Recurrences { get; set; } } public partial class R

所以我有3个实体类:

public partial class Event
{
    public Event()
    {
        Recurrences = new HashSet<Recurrence>();
    }
    public int Id { get; set; }
    public ICollection<Recurrence> Recurrences { get; set; }
}

public partial class Recurrence
{
    public Recurrence()
    {
        AspNetUsers = new HashSet<AspNetUser>();
    }
    public int Id { get; set; }
    public int EventId { get; set; }
    public ICollection<AspNetUser> AspNetUsers { get; set; }
}

public partial class AspNetUser
{
    public AspNetUser()
    {
        Recurrences = new HashSet<Recurrence>();
    }
    public string Id { get; set; }
    public string UserName { get; set; }
    public ICollection<Recurrence> Recurrences { get; set; }
}
公共部分类事件
{
公共活动()
{
Recurrences=新HashSet();
}
公共int Id{get;set;}
公共ICollection重复出现{get;set;}
}
公共部分类递归
{
公众复发()
{
AspNetUsers=newhashset();
}
公共int Id{get;set;}
public int EventId{get;set;}
公共ICollection AspNetUsers{get;set;}
}
公共部分类AspNetUser
{
公共AspNetUser()
{
Recurrences=新HashSet();
}
公共字符串Id{get;set;}
公共字符串用户名{get;set;}
公共ICollection重复出现{get;set;}
}
我想使用line to entity获取给定aspnetuser.id的事件。到目前为止,这是我得到的,但它返回了一个错误:

// GET: api/Events?userId={userId}
    public IQueryable<Event> GetEvents(string userId)
    {
        return db.Events
             .Include(e => e.Recurrences
                .Select(u => u.AspNetUsers.Where(i => i.Id == userId)));
    }
//GET:api/Events?userId={userId}
公共IQueryable GetEvents(字符串用户ID)
{
返回db.Events
.包括(e=>e.复发
.Select(u=>u.AspNetUsers.Where(i=>i.Id==userId));
}
当我排除where子句时,它可以正常工作。请帮忙。 提前谢谢

我不认为Include()的意思和你认为的一样。()它所做的是告诉数据库集确保为该对象引入关系。默认情况下(上次我选中),db上下文将自动拉入所有关系,因此这不是必需的。但是,如果您已经关闭了延迟加载(),那么您需要在查询中包含所有您想要的关系

这应该能解决你的问题。不过,我不能保证生成的SQL不会愚蠢

如果已启用延迟加载:

db.Events.Include("Recurrences").Include("Recurrences.AspNetUsers")
  .Where(e => e.Recurrences
    .Any(r => r.AspNetUsers
      .Any(u => u.Id ==userId)));
如果已关闭延迟加载:

db.Events
  .Where(e => e.Recurrences
    .Any(r => r.AspNetUsers
      .Any(u => u.Id ==userId)));
此外,如果您在查看错误时遇到困难,可以在返回查询之前将其.ToList()保存,以便它在代码中失败,而不会深入Web API堆栈。就我个人而言,我喜欢这样做,以便尝试/捕获查询并正确处理它。

我认为Include()的含义与您认为的不同。()它所做的是告诉数据库集确保为该对象引入关系。默认情况下(上次我选中),db上下文将自动拉入所有关系,因此这不是必需的。但是,如果您已经关闭了延迟加载(),那么您需要在查询中包含所有您想要的关系

这应该能解决你的问题。不过,我不能保证生成的SQL不会愚蠢

如果已启用延迟加载:

db.Events.Include("Recurrences").Include("Recurrences.AspNetUsers")
  .Where(e => e.Recurrences
    .Any(r => r.AspNetUsers
      .Any(u => u.Id ==userId)));
如果已关闭延迟加载:

db.Events
  .Where(e => e.Recurrences
    .Any(r => r.AspNetUsers
      .Any(u => u.Id ==userId)));

此外,如果您在查看错误时遇到困难,可以在返回查询之前将其.ToList()保存,以便它在代码中失败,而不会深入Web API堆栈。就个人而言,我喜欢这样做,这样我就可以尝试/捕获查询并正确处理它。

非常感谢。这解决了我的问题。现在下一个挑战是保存事件实体。非常感谢。这解决了我的问题。现在下一个挑战是保存事件实体。非常感谢。这解决了我的问题。现在,下一个挑战是保存事件实体。