Passing, persisting, and returning anonymous types
This is what I am trying to do:
staticvoid Main(string[] args){
var t =new{Number=5,Name="test"};
Test(v);
}
staticvoid Test(object v){
Console.WriteLine("{0}",t.Number);
}
Is this possible or must the dynamic (anonymous) class be used within the scope of the method it is declared in. Since i cant use var as a field, I am having a hard time understanding what to do with a projected type such as in the example below:
var storelist =from storeinProgram.DataContext.Stores
from regioninProgram.DataContext.TaxRegions
where store.TaxRegion == region.RegionID
&& store.StoreID == storeid
select new {StoreInfo=store,RegionName=region.RegionName, Tax=region.RegionTax};
In the example, I return a union of Stores and TaxRegions where the RegionIds match. I need this data for the lifetime of the application so that I am not querying continuously.
Must I define a class to hold the data from the join? Is converting the data to xml the prescribed solution to passing these types back to a caller? Is there a way to persist structures like this outside the scope of a method now, if not will there be in the final release? Can,or should) I use datasets in this scenario?
It is possible to pass an anonymous type out of a method as object. You can then use reflection to access the members of the method. For a sample doing this, look at the ObjectDumper class that comes with the LINQ preview bits. In particular, look at the privatevoid WriteObject(string prefix, object o) implementation. Naturally, you would be incurring the performance hit of reflection in this case. That is just one of the downsides of not strongly typing your results.
It appears that the "best" way to pass results around in LINQ is using strongly typed objects, otherwise the DLINQ implementation would likely use datasets/xml. Naturally, the optimal solution, as is the case in any architecture is, "it depends". Your Use Cases should dictate the architecture you need, not the technology.
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
I have a similar concern.
I will be retrieving some data into an anonymous type in "blahblah.aspx" , and then I will be stuffing that data into the "DataSource" property of a Web User Control residing on that page to display it.
What do you suggest is the ideal solution for this problem? Should I just expose the nested repeater (inside the Web User Contro)'s DataSource property to bubble it up to blahblah.aspx, or should I create strong "carrier classes" instead of using the anonymous types?
I'm leaning toward using the additional business classes to provide shape for the anonymous type query results, but I'm wondering if someone could discuss the pros and cons of either alternative.
Another suggestion:
staticvoid Main(string[] args)
{
var t = new Widget() {Number=5,Name="test"};
Test(v);
}
staticvoid Test(object o){
Widjet{int Number, string Name} w = o as Widjet{int Number, string Name};
Console.WriteLine(w.Number);
}
This solution allow casting annonymous types to other annonymous types without a hinch.
To me it seems that Widjet{int Number, string Name} is already a concrete type (anonymous or no). Why? well wouldn't the Widjet type be converted at some point into something like this:
public class Widget{
public int Number;
pulbic string Name;
}
Hence in this case the 'Widjet' part of the name is really irrelevant. Even if I created a type:
Thing{int Number; stringName}
it should still be recognized as a type with two public fields: one int named Number; one string named Name. Meaning a conversion would still be implictly possible. There just does not seem to be any reason for one not to be able to pass
class{int Number, string Name} as a parameter or declare class{int Number, string Name} x; as field.
I'm confused