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?

[3401 byte] By [JerodMoemeka] at [2007-12-24]
# 1

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

jwooley at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 2
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.

KevinHoffman at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 3
dude i think the fact that these anonymous types can't be passed around like tupples blows. It's nice and convenient but i cant see where it will ever realy be used since you'll be forced to create the concrete versions of them if you ever want to use them for anything! So when the heck is the point? I really really - taylor approximation hope - that the syntax is added to c#3.0 like how it exists in c-omega!
JerodMoemeka at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 4
Here's a language-enhancement suggestion on how MS could fix this simply:

staticvoid Main(string[] args)
{
var
t = newpublic Widget() {Number=5,Name="test"};

Test(v);
}

staticvoid Test(Widget w){

Console.WriteLine(w.Number);
}

The scope modifier (ie public) in "new public Widget" would tell the compiler to implicitly write a type called Widget. Rather like an anonymous type, but named :)

Joe

JoeAlbahari at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 5

Hi,

i think that is a ridiculous solution if thats the only way it can be done! I hope the next version of C# linq comes with will expose those anonynous types as parameters or I dont think they are going to be much help to anyone. Why can't each anonymous type you create be a definition for itself, after all isn't every dynamic instance {int, string} of a class with two public fields (one int, one string) ?!?!?!

talk to me people cuz I dont get it.

JerodMoemeka at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 6

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.

yaakov at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 7
What I ended up doing was creating IQueryable<StrongResults> classes, and then turning my queries into that by doing q.ToQueryable() , this wraps the query in an expression tree that can then be passed around and doesn't actually execute it.

It has been working quite well for me.

KevinHoffman at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 8

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

JerodMoemeka at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...

Visual Studio Orcas

Site Classified