Linq到条件不起作用的实体

时间:2014-03-28 07:53:20

标签: entity-framework entity-framework-4 linq-to-entities

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

where条件StatusReport.StatusId == 1无效。该查询返回StatusReport.StatusId不等于1的行。

为什么忽略这种情况?

1 个答案:

答案 0 :(得分:1)

因为您没有使用m加入/关联StatusReport。结果只包含m。请参见示例http://msdn.microsoft.com/en-us/library/bb311040.aspx