Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am going to cheat of @Serv and use a part of the code he used for defining data (<em>sigh</em> - yeah I am lazy).</p> <p>To handle the <code>dynamic</code> aspect of having many compliances you can use <a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>ExpandoObject</code></a>. All you need to do is to get all existing compliance types and for each type assign a property to your object.</p> <pre><code>void Main() { var content = new List&lt;Content&gt;() { new Content("I1", "V1", "C1", true), new Content("I1", "V1", "C2", false), new Content("I1", "V1", "C3", true), new Content("I1", "V2", "C1", true), new Content("I1", "V2", "C2", true), new Content("I2", "V1", "C3", true), new Content("I2", "V1", "C4", true) }; var compliances = content.Select(c=&gt;c.Compliance).Distinct(); var temp = content.GroupBy (c =&gt; new {Item = c.Item, Vendor = c.Vendor}); var results = temp.Select (t =&gt; { dynamic result = new ExpandoObject(); result.Item = t.Key.Item; result.Vendor = t.Key.Vendor; foreach(var compliance in compliances) { var isCompliant = t.Any(x =&gt; x.Compliance == compliance &amp;&amp; x.Status.GetValueOrDefault()); ((IDictionary&lt;String, Object&gt;)result).Add(compliance, isCompliant); } return result; }).Dump(); } public class Content { public Content(string item, string vendor, string compliance, bool? status) { Item = item; Vendor = vendor; Compliance = compliance; Status = status; } public string Item { get; set; } public string Vendor { get; set; } public string Compliance { get; set; } public bool? Status { get; set; } } </code></pre> <p>And the results:</p> <p><img src="https://i.stack.imgur.com/qJNlT.png" alt="Results"></p> <p><strong>EDIT</strong></p> <p>To get <code>null</code> instead of <code>false</code> when a compliance does not exist use this code the <code>foreach</code> loop:</p> <pre><code>var isCompliant = t.FirstOrDefault(x =&gt; x.Compliance == compliance); ((IDictionary&lt;String, Object&gt;)result).Add(compliance, isCompliant == null ? null : isCompliant.Status); </code></pre> <p>The results will look like this:</p> <p><img src="https://i.stack.imgur.com/xqtrk.png" alt="Results after edit."></p>
 

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