Stuck getting a Lambda Expression Correct (EF6)

I have a table called tbl_user_pass_list – a company can have multiple entries in this list, the discriminator is called usage1 and could be "Domain Administrator", "Printer", "Users" and lots of others.

I also have a table called tbl_equipment, that contains a list of servers and a lot of extended other information.

I need to generate a list of servers, with their "Domain Administrator" username and password in the resultset.

This is the code I have so far – it works, but if the Company does NOT have a "Domain Administrator" option in tbl_user_pass_list, it misses the server entirely. I need my list to have the Company and Server regardless of whether it has an entry. If a company has an entry in tbl_user_pass_list it should list the values, if a company doesn't have an entry in tbl_user_pass_list it should just return null for Username and Password.

The code below works, but when the "Domain Administrator" is not there for a particular company, it is skipping the row entirely. I can understand this from a perspective of I am explicitly ruling it out with the where clause, but I'm struggling to see how to structure this to get the results I need.

Any comments, advice would be much appreciated:

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

by TNTGav via /r/csharp

Leave a Reply