# LINQ SQL query to display the status of tracked computers

This is my existing LINQ SQL. At the moment it works, however the using the coalescing operator for null results on the "let" clauses make me feel like I'm violating some sort of unwritten rule. Is there a better way?

Also, as this is on a web server, I don't know if this should be set static or not. Methinks not. But how do I get around that? It winds up being bound to a GridView.

public static List<TrackingComputer> TrackingBoardPCsgroup(string groupName)
{

using (dbiemrDashboardDataContext db = new dbiemrDashboardDataContext())
{

var query =
(from t in TrackingBoardPCs()
where t.TrackingGroup == groupName
join p in db.GetTable<tblTBHealthPing>() on t.ComputerAsset equals p.ComputerAsset
into pingInfo
join h in db.GetTable<tblTBHealthHeartbeat>() on t.ComputerAsset equals h.ComputerAsset
into heartbeatInfo
let p = pingInfo.OrderByDescending(p => p.HealthPingTime).DefaultIfEmpty().First() ?? new tblTBHealthPing() { ComputerAsset = "Not Found", HealthPingResult = false, HealthPingTime = DateTime.Now}
let h = heartbeatInfo.OrderByDescending(p => p.HealthHeartbeatTime).DefaultIfEmpty().First() ?? new tblTBHealthHeartbeat(){ ComputerAsset = "Not Found", HealthCurrentUser = "Not Found", HealthCurrentWindow = "Not Found", HealthHeartbeatTime = DateTime.Now, HealthUptime = string.Empty}
let tc = new TrackingComputer
{
ComputerName = t.ComputerAsset,
Location = t.Location,
Pingable = p.HealthPingResult ?? false,
PingTime = p.HealthPingTime ?? DateTime.Now.AddDays(-100),
HeartbeatTime = h.HealthHeartbeatTime ?? DateTime.Now.AddDays(-100)
}
orderby tc.Pingable
select tc).ToList<TrackingComputer>();

return query;

}

}

• Don't you have any relations defined for the tables, so you don't have to make explicit joins? – Mattias Åslund Apr 28 '16 at 17:46
• Are the default values in your coalesced properties necessary for business logic or just the UI? If it's the latter, I would suggest leaving the values null and then use TargetNullValue in your bindings, e.g., {Binding Username, TargetNullValue = 'Not found'} – Dan Lyons Apr 28 '16 at 17:49
• @MattiasÅslund In reality, I don't really understand much about relations. I'd really like to learn though, do you know of anywhere that could teach me? There are relations between the tables, that is, asset numbers of the computers and IP addresses. I don't know how to link them together, or how I would write this query after having done so. – Lewis Cianci Apr 28 '16 at 22:24