title: How to combine LINQ querys with different where conditions
body: <p>I have three LINQ querys I'd like to combine. Each query has a different where condition but the group by and select clauses always follow the same pattern. Any help is greatly appreciated!</p>
  
  <p><strong>Query 1:</strong></p>
  
  <pre><code>    var querySales = from row in bookings.AsEnumerable()
                           where (row.Field&lt;Int32&gt;("t-account") &gt;= tAccSalesFrom &amp;&amp; row.Field&lt;Int32&gt;("t-account") &lt;= tAccSalesTo)
                           group row by new
                           {
                               year = row.Field&lt;DateTime&gt;("Date").Year,
                               month = row.Field&lt;DateTime&gt;("Date").Month
                           } into grp
                           orderby grp.Key.year, grp.Key.month
                           select new
                           {
                               Year = grp.Key.year,
                               Month = grp.Key.month,
                               Sales = grp.Sum(row =&gt; row.Field&lt;Decimal&gt;("Sales_Assets") - row.Field&lt;Decimal&gt;("Sales_Debit"))
                           };
  </code></pre>
  
  <p><strong>Query 2:</strong></p>
  
  <pre><code>    var queryLabourCosts = from row in bookings.AsEnumerable()
                                 where (row.Field&lt;Int32&gt;("t-account") &gt;= tAccLabFrom &amp;&amp; row.Field&lt;Int32&gt;("t-account") &lt;= tAccLabTo)
                                 group row by new
                                 {
                                     year = row.Field&lt;DateTime&gt;("Date").Year,
                                     month = row.Field&lt;DateTime&gt;("Date").Month
                                 } into grp
                                 orderby grp.Key.year, grp.Key.month
                                 select new
                                 {
                                     Year = grp.Key.year,
                                     Month = grp.Key.month,
                                     LabourCosts = grp.Sum(row =&gt; row.Field&lt;Decimal&gt;("Sales_Debit") - row.Field&lt;Decimal&gt;("Sales_Assets"))
                                 };
  </code></pre>
  
  <p><strong>Query 3:</strong></p>
  
  <pre><code>    var queryMaterial = from row in bookings.AsEnumerable()
                              where (row.Field&lt;Int32&gt;("t-account") &gt;= tAccMatFrom &amp;&amp; row.Field&lt;Int32&gt;("t-account") &lt;= tAccMatTo)
                              group row by new
                              {
                                  year = row.Field&lt;DateTime&gt;("Date").Year,
                                  month = row.Field&lt;DateTime&gt;("Date").Month
                              } into grp
                              orderby grp.Key.year, grp.Key.month
                              select new
                              {
                                  Year = grp.Key.year,
                                  Month = grp.Key.month,
                                  MaterialCosts = grp.Sum(row =&gt; row.Field&lt;Decimal&gt;("Sales_Debit") - row.Field&lt;Decimal&gt;("Sales_Assets"))
                              };
  </code></pre>
  
  <p><strong>Solution: Thanks to lazyberezovsky!</strong></p>
  
  <pre><code>                var querySalesLabMat = from b in bookings.AsEnumerable()
                                 group b by new
                                 {
                                     b.Field&lt;DateTime&gt;("Date").Year,
                                     b.Field&lt;DateTime&gt;("Date").Month,
                                 } into g
                                 orderby g.Key.Year, g.Key.Month
                                 select new
                                 {
                                     g.Key.Year,
                                     g.Key.Month,
                                     Sales = g.Where(r =&gt; r.Field&lt;Int32&gt;("t-account") &gt;= tAccSalesFrom &amp;&amp; r.Field&lt;Int32&gt;("t-account") &lt;= tAccSalesTo)
                                                      .Sum(r =&gt; r.Field&lt;Decimal&gt;("Sales_Assets") - r.Field&lt;Decimal&gt;("Sales_Debit")),
                                     LabourCosts = g.Where(r =&gt; r.Field&lt;Int32&gt;("t-account") &gt;= tAccLabFrom &amp;&amp; r.Field&lt;Int32&gt;("t-account") &lt;= tAccLabTo)
                                                    .Sum(r =&gt; r.Field&lt;Decimal&gt;("Sales_Debit") - r.Field&lt;Decimal&gt;("Sales_Assets")),
                                     MaterialCosts = g.Where(r =&gt; r.Field&lt;Int32&gt;("t-account") &gt;= tAccMatFrom &amp;&amp; r.Field&lt;Int32&gt;("t-account") &lt;= tAccMatTo)
                                                    .Sum(r =&gt; r.Field&lt;Decimal&gt;("Sales_Debit") - r.Field&lt;Decimal&gt;("Sales_Assets"))
                                 };
  </code></pre>
  
  <p>Thanks in advance for your help,
  Mat</p>