Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;


        }


    }
share|improve this question
    
Don't you have any relations defined for the tables, so you don't have to make explicit joins? – Mattias Åslund Apr 28 at 17:46
1  
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 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 at 22:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.