ONYX
ONYX

Reputation: 5859

Linq To Entities

I have a small problem in my where clause in the linq expression below. If I put the number 3 instead of department.Id I get the desired result but when I use department.Id I get nothing in the resultset.

I also want to get a count for the number of filters for that filter name using the query again using distinct.

var dept = Page.RouteData.Values["department"];
var department = (from d in db.Departments 
                  where d.Name.Replace(" ", "-") == dept 
                  select new {d.Id, d.Name}).FirstOrDefault();


var query = from p in db.Products
            join f in db.ProductFilters on p.Id equals f.ProductId into filters
            from x in filters.Where(x => x.Product.DepartmentId == department.Id 
            /* if == 3 it works */)                            
            select new { x.Name, x.Id };     

Upvotes: 2

Views: 101

Answers (2)

InBetween
InBetween

Reputation: 32780

Your first query is not finding any valid department and is therefore returning default which most probably means that departmend.Id == 0.

Upvotes: 0

Jon Egerton
Jon Egerton

Reputation: 41569

Promoted to answer from comments:

Have you checked that the department instance is as you think it should be after the first linq statement - ie has an Id == 3?

Upvotes: 2

Related Questions