Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't believe that you will be able to achieve this. Although when you do <code>select new { c.Name, c.Population }</code> it seems like you're not creating a class you actually are. If you have a look at the compiled output in Reflector or the raw IL you will be able to see this.</p> <p>You'll have a class which would look something like this:</p> <pre><code>[CompilerGenerated] private class &lt;&gt;c__Class { public string Name { get; set; } public int Population { get; set; } } </code></pre> <p>(Ok, I cleaned it up a touch, since a property is really just a <code>get_Name()</code> and <code>set_Name(name)</code> method set anyway)</p> <p>What you're trying to do is proper dynamic class creation, something which wont be available until .NET 4.0 comes out (and even then I'm not really sure if it'll be able to achieve what you want).</p> <p>You're best solution would be to define the different <em>anonymous</em> classes and then have some kind of logical check to determine which one to create, and to create it you can use the object <code>System.Linq.Expressions.NewExpression</code>.</p> <p>But, it may be (in theory at least) possible to do it, if you're getting really hard-core about the underlying LINQ provider. If you <strong>are</strong> writing your own LINQ provider you can detect if the currently-parsed expression is a Select, then you determine the <code>CompilerGenerated</code> class, reflect for its constructor and create.</p> <p>Defiantly not a simple task, but it would be how LINQ to SQL, LINQ to XML, etc all do it.</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.
 

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