1
\$\begingroup\$

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,
          IPAddress = t.ComputerIP,
          Location = t.Location,
          Pingable = p.HealthPingResult ?? false,
          PingTime = p.HealthPingTime ?? DateTime.Now.AddDays(-100),
          Username = h.HealthCurrentUser ?? "Not Found",
          CurrentWindow = h.HealthCurrentWindow ?? "Not Found",
          Uptime = h.HealthUptime ?? "Not Found",
          HeartbeatTime = h.HealthHeartbeatTime ?? DateTime.Now.AddDays(-100)
      }
      orderby tc.Pingable
      select tc).ToList<TrackingComputer>();



            return query;


        }


    }
\$\endgroup\$
  • \$\begingroup\$ Don't you have any relations defined for the tables, so you don't have to make explicit joins? \$\endgroup\$ – Mattias Åslund Apr 28 '16 at 17:46
  • 1
    \$\begingroup\$ 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'} \$\endgroup\$ – Dan Lyons Apr 28 '16 at 17:49
  • \$\begingroup\$ @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. \$\endgroup\$ – Lewis Cianci Apr 28 '16 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.