title: Sum a column in SQL or C#
body: <p>I have a question for the TSQL experts out there or the LINQ C# experts out there. I have a stored procedure that joins in a few tables. Basically Name, ID, and about 10 product columns. The stored procedure can return a lot of data (90,000+ rows sometimes). In the stored proc I filter the results to filter out all rows where all of the products in that row = 0. What I need to do is to remove columns in C# where all of the values in a column (product) are zero. This is due to some business rules within my application (customer request). </p>
  
  <p>The application can remove (hide) columns by sending an array of the column names to a report builder. For example:</p>
  
  <p>|Name|ID|Prod1|Prod2|Prod3|Prod4|</p>
  
  <p>If I wanted to remove Prod3 and Prod4 from the report:</p>
  
  <pre><code>List&lt;string&gt; hideColumns = new List&lt;string&gt;();
  hideColumns.Add("Prod3");
  hideColumns.Add("Prod4");
  </code></pre>
  
  <p>and I would send
      hideColumns.ToArray()
  to the report builder and those columns would be removed. </p>
  
  <p>In my application I send in all of the data, then use the following LINQ to find the column values for the returned data.</p>
  
  <pre><code>var zeroCols = from result in data
                 group result by new { } into C
                 select new ProductDataClass
                 {
                     Prod1 = C.Sum(x =&gt; x.Prod1)
                     Prod2 = C.Sum(x =&gt; x.Prod2)
                     ...etc to Prod10
                 };
  </code></pre>
  
  <p>To find which is 0</p>
  
  <pre><code>if (zeroCols.Count() &gt; 0)
  {
      if (zeroCols.First().Prod1 == 0)
          hideColumns.Add("Prod1");
      ...etc to Prod10
  }
  </code></pre>
  
  <p>However, my problem is that the LINQ is really slow if the report returns a lot of rows. Can anybody make any suggestions?</p>