Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Marc's answer gives sparse matrix that can't be pumped into Grid directly.<br> I tried to expand the code from the <a href="http://www.extensionmethod.net/Details.aspx?ID=147" rel="noreferrer">link provided by Vasu</a> as below:</p> <pre><code>public static Dictionary&lt;TKey1, Dictionary&lt;TKey2, TValue&gt;&gt; Pivot3&lt;TSource, TKey1, TKey2, TValue&gt;( this IEnumerable&lt;TSource&gt; source , Func&lt;TSource, TKey1&gt; key1Selector , Func&lt;TSource, TKey2&gt; key2Selector , Func&lt;IEnumerable&lt;TSource&gt;, TValue&gt; aggregate) { return source.GroupBy(key1Selector).Select( x =&gt; new { X = x.Key, Y = source.GroupBy(key2Selector).Select( z =&gt; new { Z = z.Key, V = aggregate(from item in source where key1Selector(item).Equals(x.Key) &amp;&amp; key2Selector(item).Equals(z.Key) select item ) } ).ToDictionary(e =&gt; e.Z, o =&gt; o.V) } ).ToDictionary(e =&gt; e.X, o =&gt; o.Y); } internal class Employee { public string Name { get; set; } public string Department { get; set; } public string Function { get; set; } public decimal Salary { get; set; } } public void TestLinqExtenions() { var l = new List&lt;Employee&gt;() { new Employee() { Name = "Fons", Department = "R&amp;D", Function = "Trainer", Salary = 2000 }, new Employee() { Name = "Jim", Department = "R&amp;D", Function = "Trainer", Salary = 3000 }, new Employee() { Name = "Ellen", Department = "Dev", Function = "Developer", Salary = 4000 }, new Employee() { Name = "Mike", Department = "Dev", Function = "Consultant", Salary = 5000 }, new Employee() { Name = "Jack", Department = "R&amp;D", Function = "Developer", Salary = 6000 }, new Employee() { Name = "Demy", Department = "Dev", Function = "Consultant", Salary = 2000 }}; var result5 = l.Pivot3(emp =&gt; emp.Department, emp2 =&gt; emp2.Function, lst =&gt; lst.Sum(emp =&gt; emp.Salary)); var result6 = l.Pivot3(emp =&gt; emp.Function, emp2 =&gt; emp2.Department, lst =&gt; lst.Count()); } </code></pre> <p>* can't say anything about the performance though.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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