StackTrace
StackTrace

Reputation: 9416

Linq to entities where condition not working

var records = (from m in ctx.myData
               from StatusReport in ctx.ReportStatusDetails
               where (m.UserId == user.UserId && 
                      StatusReport.StatusId == 1 &&
                      ctx.Report.Any(r => r.ReportDate.Month == SqlFunctions.GetDate().Value.Month &&
                                          r.ReportDate.Year == SqlFunctions.GetDate().Value.Year))
               select new
               {
                   m.Id,
                   m.Company.CompanyName,
                   m.UserId,
                   m.aspnet_Membership.aspnet_Users.UserName,
                   m.aspnet_Membership.Email
               })
               .Distinct()
               .ToList();

The where condition StatusReport.StatusId == 1 is not working. The query is returning even rows where StatusReport.StatusId is not equal to 1.

Why is this condition being ignored?

Upvotes: 0

Views: 173

Answers (1)

Ivan Doroshenko
Ivan Doroshenko

Reputation: 942

Because you did not joined/related StatusReport with m. And result contains only m. See example http://msdn.microsoft.com/en-us/library/bb311040.aspx

Upvotes: 1

Related Questions