title: Find frequency of values in an Array or XML (C#)
body: <p>I have an XML feed (which I don't control) and I am trying to figure out how to detect the volume of certain attribute values within the document.</p>
  
  <p>I am also parsing the XML and separating attributes into Arrays (for other functionality) </p>
  
  <p>Here is a sample of my XML</p>
  
  <pre><code>&lt;items&gt;
  &lt;item att1="ABC123" att2="uID" /&gt;
  &lt;item att1="ABC345" att2="uID" /&gt;
  &lt;item att1="ABC123" att2="uID" /&gt;
  &lt;item att1="ABC678" att2="uID" /&gt;
  &lt;item att1="ABC123" att2="uID" /&gt;
  &lt;item att1="XYZ123" att2="uID" /&gt;
  &lt;item att1="XYZ345" att2="uID" /&gt;
  &lt;item att1="XYZ678" att2="uID" /&gt;
  &lt;/items&gt;
  </code></pre>
  
  <p>I want to find the volume nodes based on each att1 value. Att1 value will change. Once I know the frequency of att1 values I need to pull the att2 value of that node.</p>
  
  <p>I need to find the TOP 4 items and pull the values of their attributes.</p>
  
  <p>All of this needs to be done in C# code behind.</p>
  
  <p>If I was using Javascript I would create an associative array and have att1 be the key  and the frequency be the value. But since I'm new to c# I don't know how to duplicate this in c#.</p>
  
  <p>So I believe, first I need to find all unique att1 values in the XML. I can do this using:</p>
  
  <pre><code>IEnumerable&lt;string&gt; uItems = uItemsArray.Distinct();
  // Where uItemsArray is a collection of all the att1 values in an array
  </code></pre>
  
  <p>Then I get stuck on how I compare each unique att1 value to the whole document to get the volume stored in a variable or array or whatever data set. </p>
  
  <p>Here is the snippet I ended up using:</p>
  
  <pre><code>        XDocument doc = XDocument.Load(@"temp/salesData.xml");
          var topItems = from item in doc.Descendants("item")
                      select new
                      {
                          name = (string)item.Attribute("name"),
                          sku = (string)item.Attribute("sku"),
                          iCat = (string)item.Attribute("iCat"),
                          sTime = (string)item.Attribute("sTime"),
                          price = (string)item.Attribute("price"),
                          desc = (string)item.Attribute("desc")
  
                      } into node
                      group node by node.sku into grp
                      select new { 
                          sku = grp.Key,
                          name = grp.ElementAt(0).name,
                          iCat = grp.ElementAt(0).iCat,
                          sTime = grp.ElementAt(0).sTime,
                          price = grp.ElementAt(0).price,
                          desc = grp.ElementAt(0).desc,
                          Count = grp.Count() 
                      };
  
          _topSellers = new SalesDataObject[4];
          int topSellerIndex = 0;
          foreach (var item in topItems.OrderByDescending(x =&gt; x.Count).Take(4))
          {
              SalesDataObject topSeller = new SalesDataObject();
              topSeller.iCat = item.iCat;
              topSeller.iName = item.name;
              topSeller.iSku = item.sku;
              topSeller.sTime = Convert.ToDateTime(item.sTime);
              topSeller.iDesc = item.desc;
              topSeller.iPrice = item.price;
              _topSellers.SetValue(topSeller, topSellerIndex);
              topSellerIndex++;
          } 
  </code></pre>
  
  <p>Thanks for all your help!</p>