Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since you have known number of columns, you can group data by Customer and products and then take conditional sum from grouping and it will make different columns of the resultant query. Plz have a look at following LinqPad program. Sorry, i m quite handicapped at VB.Net so i have coded it in C# but you'll get the fair idea</p> <pre><code>void Main() { var Period1Start = new DateTime(2010,1,1); var Period1End = new DateTime(2010,12,31); var Period2Start = new DateTime(2011,1,1); var Period2End = new DateTime(2011,12,31); List&lt;Item&gt; lst = new List&lt;Item&gt; { new Item{ Value1 = 100, Value2 = 50, Customer = 1000, Product = 100 , Date = new DateTime(2010,8,1)}, new Item{ Value1 = 50, Value2 = 20, Customer = 1000, Product = 101 , Date = new DateTime(2010,5,1)}, new Item{ Value1 = 200, Value2 = 60, Customer = 1000, Product = 100 , Date = new DateTime(2011,2,6)}, new Item{ Value1 = 180, Value2 = 100, Customer = 1001, Product = 100 , Date = new DateTime(2010,7,3)}, new Item{ Value1 = 500, Value2 = 700, Customer = 1000, Product = 100 , Date = new DateTime(2010,1,1)}, new Item{ Value1 = 300, Value2 = 300, Customer = 1001, Product = 100 , Date = new DateTime(2011,4,4)}, new Item{ Value1 = 250, Value2 = 600, Customer = 1000, Product = 100 , Date = new DateTime(2011,3,3)} }; var grp = lst.GroupBy(x=&gt;new{x.Customer, x.Product}). Select(y=&gt; new { Customer = y.Key.Customer, Product = y.Key.Product, SumValue1Period1 = y.Where(x=&gt;x.Date &gt;= Period1Start &amp;&amp; x.Date&lt;= Period1End).Sum(p=&gt;p.Value1), SumValue2Period1 = y.Where(x=&gt;x.Date &gt;= Period1Start &amp;&amp; x.Date&lt;= Period1End).Sum(p=&gt;p.Value2), SumValue1Period2 = y.Where(x=&gt;x.Date &gt;= Period2Start &amp;&amp; x.Date&lt;= Period2End).Sum(p=&gt;p.Value1), SumValue2Period2 = y.Where(x=&gt;x.Date &gt;= Period2Start &amp;&amp; x.Date&lt;= Period2End).Sum(p=&gt;p.Value2) }); Console.WriteLine(grp); } // Define other methods and classes here public class Item { public int Value1{get;set;} public int Value2{get;set;} public int Customer{get;set;} public int Product{get;set;} public DateTime Date{get;set;} } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload