Ehi
Ehi

Reputation: 43

using a where clause in Entity Framework

I am trying to filter based on a table hierarchical relationship. But getting the error below.

I am trying to filter based on the variable C in c.Departments_Category_Registration.Category_Name == C

Can anyone advise, here is my code

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);

and error message
InvalidOperationException: Incorrect include argument: c => (c.Departments_Category_Registration.Category_Name == __C_0)

update. I changed my code to this below and get no error, but yields no results

      [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());

Upvotes: 0

Views: 86

Answers (1)

Alexey Andrushkevich
Alexey Andrushkevich

Reputation: 6162

You pass condition to the Include method. It's supposed to accept the property only. Change it to .Include(c => c.Departments_Category_Registration) and move the match by name condition under Where clause.

Upvotes: 1

Related Questions