C#Lambda with Entity Framework - 即使没有匹配项也会返回结果

时间:2015-04-22 21:57:31

标签: c# entity-framework lambda

以下代码正常运行,但如果根据where子句无法找到带有“域管理员”的条目,则它将完全忽略该特定结果上的任何其他内容。这导致我丢失项目,因为我生成的视图可能没有“域管理员”条目。我明白这是因为它是一个明确的地方,但我不太确定如何准确地表示我需要做什么。我怀疑我需要进行LEFT JOIN,但不确定这会如何影响导航属性。任何对正确方向的指导都将受到赞赏。

var endpointConstructor = db.tbl_equipment.Include(t => t.tbl_Backup_Configuration)
   .Where(e => e.tbl_Backup_Configuration.FirstOrDefault().BackupType == null)
   .Where(e => e.tbl_customer.Calc_Contract_Status == true && e.Calc_Contract_Status == true && e.Equip_type.Contains("server")).OrderBy(e => e.tbl_customer.Priority)
   .Where(what => what.tbl_customer.tbl_user_pass_list.FirstOrDefault().Usage1 == "Domain Administrator")
   .Select(s => new CompanyServerUserPassViewModel { Comp_ID = s.Comp_ID, ServerName = s.NetBIOS_name, AdminUsername = s.tbl_customer.tbl_user_pass_list.FirstOrDefault().Username,
   AdminPassword = s.tbl_customer.tbl_user_pass_list.FirstOrDefault().Password, Company = s.Company, TeamviewerID = s.tbl_computerinfo.FirstOrDefault().teamviewerID });

1 个答案:

答案 0 :(得分:0)

有些事情:

.Where(what => [statement that evaluates to true if there is no domain admin] 
|| what.tbl_customer.tbl_user_pass_list.First().Usage1 == "Domain Administrator")