Asp.net core 在实体框架中使用where子句
我正在尝试根据表层次关系进行筛选。但是得到下面的错误 我试图根据Asp.net core 在实体框架中使用where子句,asp.net-core,Asp.net Core,我正在尝试根据表层次关系进行筛选。但是得到下面的错误 我试图根据C.Departments\u Category\u Registration.Category\u Name==C 有人能告诉我吗,这是我的密码 var model = from r in _context.Departments_SubCategory_Registration.Include(c => c.Departments_Category_Registration.Category_Name == C)
C.Departments\u Category\u Registration.Category\u Name==C
有人能告诉我吗,这是我的密码
var model = from r in _context.Departments_SubCategory_Registration.Include(c => c.Departments_Category_Registration.Category_Name == C)
where r.IsEnabled == true
select r;
return View(model);
和错误消息无效操作异常:不正确的包含参数:c=>(c.Departments\u Category\u Registration.Category\u Name==\u\u c\u 0)
更新。我将代码更改为下面的代码,没有得到任何错误,但没有产生任何结果
[Route("/{C}")]
public async Task<IActionResult> Product(String C)
{
return View(await _context.Departments_SubCategory_Registration.Include(c => c.Departments_Category_Registration)
.Where (d => d.IsEnabled == true)
.Where(r => r.Departments_Category_Registration.Category_Name == C).ToListAsync());
[路由(“/{C}”)]
公共异步任务产品(字符串C)
{
返回视图(等待上下文。部门子类别注册。包括(c=>c.部门类别注册)
.Where(d=>d.IsEnabled==true)
其中(r=>r.Departments\u Category\u Registration.Category\u Name==C).ToListAsync());
您将条件传递给Include
方法。它应该只接受属性。将其更改为.Include(c=>c.Departments\u Category\u Registration)
并将名称匹配条件移动到Where
子句下。更新了上面的我的代码。我尝试了你的代码,效果很好。确保你的数据库中的记录IsEnabled为true,其部门\u Category\u Registration.Category\u name为C。你能与我们分享你的模型吗?嗨,伙计们,谢谢你的贡献。我发现问题是,虽然我已经将FK添加到了我的模型中,但我没有在Departments_SubCategory_Registration模型中添加正确的密钥名,不幸的是,它在运行时没有给出错误。现在可以工作了,非常感谢