Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand you correctly, you'd like to get a collection of objects that you don't need to define in your code but that are usable in a strongly typed fashion. Sadly, no you can't.</p> <p>An <strong>anonymous type</strong> seems like some kind of <em>variant</em> or <em>dynamic object</em>, but it is in fact a strongly typed class that is defined at compile time. .NET defines the type for you automatically behind the scenes. In order for .net to be able to do this, it has to have some clue from the code with which to infer the type definition. It has to have something like:</p> <pre><code>from ItemA in ItemData.AsEnumerable() select ItemA.Item("Name"), ItemA.Item("Email") </code></pre> <p>so it knows what members to define. There's no way to get around it, the information has to logically be there for the anonymous type to be defined.</p> <p>Depending on why exactly your are trying to do this, there are some options.</p> <ul> <li>If you want intellisense while still encapsulating your data access, you can return xml instead of a datatable from your encapsulated data access class. (You can convert data tables to xml very easily. You'll want to use the new <strong>System.Xml.Linq</strong> classes like the <a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx" rel="nofollow noreferrer">XElement</a>. They're great!) Then you can use VS2008's ability to create an xsd schema from xml. Then <em>use/import</em> that schema at the top of your code page, and you have intellisense.</li> <li>If you have to have an <em>object</em> an with properties for your data, but don't want to define a class/structure for them, you'll love the new <strong>dynamic objects</strong> coming in C#4.0/VB10. You have object properties based on what the sql returns, but you won't have intellisense. There is also a performance cost to this, but (a) that might not matter for your situation and (b) it actually is not so bad in some situations.</li> <li>If you're just trying to avoid making a lot of classes, consider defining structs/structures on the same code file, beneath your class definition. When you add more columns to your result set, it's easy to adjust a struct with more public fields.</li> </ul> <p>In short you can have any <strong>two</strong> of the following three: (a) dynamic, (b) strontly-typed objects, (3) intellisense. But not all three.</p>
    singulars
    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.
 

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