Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is not possible to infer the types the way you want. You have a few choices though:</p> <ol> <li><p>Make a additional class (which you don't want)</p></li> <li><p>Change the style of the class so CreateResults and PrintResults is in the same method as you have done. BUT you properly have a good reason not to do that.</p></li> <li>Use the dynamic type (I guess there is one in VB, but I don't know for sure. I'm a C# man) and then you simple access the properties you want as you do in your example. This is obviously not as fast as static typing.</li> <li>There is a hack to do it (at least in C#). But it is not pretty. See <a href="https://stackoverflow.com/questions/3149209/how-to-return-anonymous-type-while-using-linq">How to return Anonymous Type while using Linq</a></li> <li>Instead of using a custom class you could use the tuple classes. System.Tuple and store the data there. They came in .Net 4 I think.</li> </ol> <p>In the end I really think a custom class is the way to go unless you have a good reason not to. I would also like a way to do the same thing you want to, but a custom class can be described in a few lines with newer syntax.</p> <p><strong>EDIT: Added a few points and changed a little bit</strong></p> <p><strong>Example with dynamic in C# as requested</strong></p> <pre><code>class Foo { private dynamic _linqResults; public void CreateResults() { var someData = Enumerable.Range(1, 10); _linqResults = from i in someData select new { Number = i }; } public void PrintResults() { foreach (var i in _linqResults) Console.WriteLine(i.Number); } } </code></pre> <p>As you maybe see, it is almost the same syntax as you want. It just uses dynamic which is a bit slower - but it sounds like that you already uses or will use some kind of dynamic/reflection in datagrid and csv. It of cause depends on which DataGrid and CSV (your own?) you use.</p> <p>As mentioned in another answer, the compiler can't infer the types and IntelliSense will not be your guide in PrintResults.</p> <p><strong>Little update</strong> VB has a similar syntax which you can find a good walk-trough with here: <a href="http://blogs.msdn.com/b/vbteam/archive/2008/12/17/walkthrough-dynamic-programming-in-visual-basic-10-0-and-c-4-0-lisa-feigenbaum.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/vbteam/archive/2008/12/17/walkthrough-dynamic-programming-in-visual-basic-10-0-and-c-4-0-lisa-feigenbaum.aspx</a></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