我正在尝试设置一种方法,该方法返回与您注册的演示文稿不重叠的所有演示文稿。但是,当我试图实现这个时,我遇到了一个我似乎无法修复的错误,我环顾四周并且未能找到任何类似的问题。我错过了一些明显的东西吗?
这是错误:
InvalidOperationException:类型的变量't0' 'Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor + TransparentIdentifier`2 [NameSpace.Models.Presentation,Microsoft.EntityFrameworkCore.Storage.ValueBuffer]' 从范围''引用,但未定义
这些是我的模特。
public class ApplicationUser : IdentityUser
{
public List<Presentation> MyPresentations { get; set; }
public List<PresentationUser> RegisteredPresentations { get; set; }
}
public class Presentation
{
public int PresentationId { get; set; }
public string HostId { get; set; }
public ApplicationUser Host { get; set; }
public List<PresentationUser> Attendees { get; set; }
public int TimeId { get; set; }
public PresentationTime Time { get; set; }
}
public class PresentationUser
{
public int PresentationId { get; set; }
public Presentation Presentation { get; set; }
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}
public class PresentationTime
{
public int PresentationTimeId { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}
这是我无法开展工作的方法
private async Task<IQueryable<Presentation>> GetAvailablePresentations()
{
User user = await context.Users
.Include(u => u.RegisteredPresentations)
.ThenInclude(eu => eu.Presentation.Host)
.FirstOrDefaultAsync(u => u.Id == userManager.GetUserId(User));
var Presentations = context.Presentations
.Include(e => e.Host)
.Include(e => e.Time)
.Include(e => e.Attendees)
.ThenInclude(e => e.ApplicationUser)
// Filter Out Conditions
.Where(e => e.Attendees.All(u => u.ApplicationUserId != user.Id)) // Cannot see Presentations they are attending.
.Where(e => e.HostId != user.Id); // Cannot see their own Presentation
var debug = user.RegisteredPresentations.Select(ex => ex.Presentation).ToList();
// This section makes it so that users can't sign up for more that one Presentation per timeslot.
// Error Occurs Here
Presentations = Presentations.Where(e => debug.All(ex =>
ex.Time.EndTime < e.Time.StartTime || e.Time.EndTime < ex.Time.StartTime));
// This also does not work
// Presentations = Presentations.Where(e => debug.All(ex => ex.Time.StartTime != e.Time.StartTime));
return Presentations;
}
如果有人可以帮我解决这个问题,那将是非常有帮助的。
注意:我剥离了许多其他逻辑来帮助隔离此问题,因此我可能在此代码中有一些不必要的.Include()
。
答案 0 :(得分:2)
Presentations
不是列表,它仍然是IQueryable
- 尚未执行的DB查询。应用Where
指示EF在SQL中应用其他WHERE
。
但是debug
是内存中对象的列表(.ToList()
)。您如何看待EF将它们转移回DB?
如果您希望在数据库中应用所有过滤 - 您应该将debug
更改为某些内容列表&#34;简单&#34; (ID列表?) - 然后EF将能够将此列表传递回DB。
或者,您应该将所有合适的演示文稿读入内存(调用.ToList()
)并在内存中应用最后一次过滤。您可以从debug
计算min(StartTime)和max(EndTime),并将这两个简单值应用于Presentations查询(您将收到较少的不必要的项目)然后读取到内存并应用&#34; strong&#34;在内存中过滤。