title: JSON Where clause filter using Newtonsoft.Json.Linq
body: <p>I have a json string which looks something like this (simplified version):</p>
  
  <pre><code>{"Statistics":[
                  {"Direction":"2-WAY","AADT":8129,"BC":254},
                  {"Direction":"NEG","AADT":3956,"BC":124},
                  {"Direction":"POS","AADT":4173,"BC":128}
              ]
  }
  </code></pre>
  
  <p>I also have a c# program started:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Newtonsoft.Json.Linq;
  namespace TrafficCounterJSON {
      class Program {
          static void Main(string[] args) {
              string jsonString = "{\"Statistics\":[{\"Direction\":\"2-WAY\",\"AADT\":8129},{\"Direction\":\"NEG\",\"AADT\":3956}]}";
              var jObjectt = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
          }//main
  
     }
  }
  </code></pre>
  
  <p>I would like to know how I could run a know how to use Newtonsoft.Json.Linq to query the items in the json string as follows:</p>
  
  <p>I want all the statistics rows where "Direction = NEG"</p>
  
  <p>I cannot seem to figure out the correct syntax to get those results (via a select ... where clause)</p>
  
  <p>So based on input below, I finally just built a model for the json data (I used this site: <a href="http://json2csharp.com" rel="nofollow noreferrer">http://json2csharp.com</a>)</p>
  
  <p>Below is a fuller version of the json:</p>
  
  <pre><code> {  "RequestId":"068f38ed-532c-442c-aae7-748112926b0d",
  
      "Parameters":{"AgencyId":25,"LocalId":"710541"},
  
      "Statistics":[
          {"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"2-WAY","AADT":8129,"BC":254,"TimePeriod":"AM","PeakHour":"07:00 - 08:00","PeakHourVolume":832,"CommercialPeakHourVolume":24},
          {"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"2-WAY","AADT":8129,"BC":254,"TimePeriod":"PM","PeakHour":"16:00 - 17:00","PeakHourVolume":863,"CommercialPeakHourVolume":16},
          {"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"NEG","AADT":3956,"BC":124,"TimePeriod":"AM","PeakHour":"07:00 - 08:00","PeakHourVolume":234,"CommercialPeakHourVolume":9},
          {"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"NEG","AADT":3956,"BC":124,"TimePeriod":"PM","PeakHour":"16:00 - 17:00","PeakHourVolume":525,"CommercialPeakHourVolume":7},
          {"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"POS","AADT":4173,"BC":128,"TimePeriod":"AM","PeakHour":"07:00 - 08:00","PeakHourVolume":598,"CommercialPeakHourVolume":15},
          {"LastCollectionDate":"\/Date(1479272400000-0500)\/","Direction":"POS","AADT":4173,"BC":128,"TimePeriod":"PM","PeakHour":"16:00 - 17:00","PeakHourVolume":338,"CommercialPeakHourVolume":9}
      ],
      "AadtHistory":[
          {"Year":2017,"Aadt":8202,"Source":"Grown from 2016"},
          {"Year":2016,"Aadt":8129,"Source":null},
          {"Year":2015,"Aadt":6194,"Source":"Grown from 2014"},
          {"Year":2014,"Aadt":6061,"Source":null},
          {"Year":2013,"Aadt":5538,"Source":"Grown from 2012"},
          {"Year":2012,"Aadt":5617,"Source":"Grown from 2011"},
          {"Year":2011,"Aadt":5623,"Source":null},
          {"Year":2000,"Aadt":6090,"Source":"Flowmap"},
          {"Year":1997,"Aadt":5210,"Source":"Flowmap"},
          {"Year":1992,"Aadt":5360,"Source":"Flowmap"},
          {"Year":1988,"Aadt":4330,"Source":"Flowmap"}]
      }
  </code></pre>
  
  <p>Here is the corresponding model I am now using:</p>
  
  <pre><code>public class Parameters {
      public int AgencyId { get; set; }
      public string LocalId { get; set; }
  }
  
  public class Statistic {
      public DateTime LastCollectionDate { get; set; }
      public string Direction { get; set; }
      public int AADT { get; set; }
      public int BC { get; set; }
      public string TimePeriod { get; set; }
      public string PeakHour { get; set; }
      public int PeakHourVolume { get; set; }
      public int CommercialPeakHourVolume { get; set; }
  }
  
  public class AadtHistory {
      public int Year { get; set; }
      public int Aadt { get; set; }
      public string Source { get; set; }
  }
  
  public class RootObject {
      public string RequestId { get; set; }
      public Parameters Parameters { get; set; }
      public List&lt;Statistic&gt; Statistics { get; set; }
      public List&lt;AadtHistory&gt; AadtHistory { get; set; }
  }
  </code></pre>
  
  <p>Here is a small sample of the code I am using the pull data from rows in the Statistics array (from the json string)</p>
  
  <pre><code>var myObject = JsonConvert.DeserializeObject&lt;RootObject&gt;(jsonResponseString);
       foreach (Statistic stat in myObject.Statistics) {
         Console.WriteLine("AADT:" + stat.AADT);                        
  }
  </code></pre>