Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate LINQ query at runtime to GroupBy in EntityFramework (with inheritance)
    text
    copied!<p>This is the scenario. I have the following three classes, they are defined in Entity Framework, i only define them here for the example:</p> <pre><code>public class Foo { public string Color { get; set; } } public class Bar : Foo { public string Height { get; set; } } public class Pipe : Foo { public string Width { get; set; } } </code></pre> <p>So, I have many Foo's, this is my base class, I want to be able to specify a propery, and do this query:</p> <pre><code> from e in Context.Foos group e.Color by e.Color into result select new { Value = result.Key, ValueCount = result.Count() } </code></pre> <p>This should end up with:</p> <p>Blue 2 Black 4 Yellow 2</p> <p>This works, however I want to specify this at run time, with the Property name 'Color' passed by the client. Also, I want to search the derived entities too. If i try to do </p> <pre><code> group e.Height by e.Height into result </code></pre> <p>It wont work because there is no Height in Foo, only in Bar. But the point is I <strong>ONLY</strong> want to return Bars, this should also be specified at runtime. This is the main problem I have been having. I cant do <code>Foos.OfType&lt;Bar&gt;.GroupBy(some dynamic stuff)</code> because I dont know the type to filter for at runtime.</p> <p>Would really appreciate some help on this matter.</p> <p><strong>EDIT</strong></p> <p>Basically, what i'm trying to do is this <a href="https://stackoverflow.com/questions/1465700/system-linq-dynamic-select-new-into-a-listt-or-any-other-enumerable">System.LINQ.Dynamic: Select(&quot; new (...)&quot;) into a List&lt;T&gt; (or any other enumerable collection of &lt;T&gt;)</a> but return Count instead of Sum at the end.</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