簡體   English   中英

具有實體框架的C#Lambda-即使沒有匹配項也返回結果

[英]C# Lambda with Entity Framework - Return a result even when no match

下面的代碼可以正常工作,但是如果無法根據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 });

類似於以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM