One of the great features of C# 3.0 are anonymous types. Anonymous Types enable developers to concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type. In C# the definition of an anonymous type makes use of another C# 3.0 new feature: the object initialize feature. The difference is that instead of declaring the type-name as part of the initialization grammar, when instantiating anonymous types you instead just leave the type-name blank after the new keyword:
var product = new
ID = 1000,
Name = "Mouse",
UnitPrice = 100,
TotalRevenue = 100000
The compiler will parse the above syntax and automatically define a new standard CLR type that has 4 properties.
Note: If all these new features don’t say a lot to you, I suggest you to have a look at the LINQ Hands On Lab I wrote last year about all the main new C# 3.0 enhancements and LINQ and then come back later to this post.
Of course anonymous types can also be used in LINQ to SQL query expression and this is exactly what I did in the following Console Application sample. I first created a LINQ to SQL object model by using the LINQ to SQL Designer provided by Visual Studio 2008, as shown in the following figures
Then I wrote inside my Console Application a really simple query that just returns the ID and the Name of customers who made more than one order, of course making use of both anonymous types and object initializers features:
Suppose now that instead of using a local anonymous type you may want to put the result of your LINQ query in an object defined by yourself. To do this I defined a new type (the MyClass type) with 2 properties (ID and Name) inside the LINQ to SQL Designer as shown in the following figures and I rebuilt the whole project.
It’s now time to make some changes to our LINQ query. What I did?
I changed the return type by replacing the var keyword with a generic List of type MyClass (List<MyClass>). Then, after the select new statement I explicitly had to say that I wanted to create a generic list of type MyClass (Note that I’m not using anymore an anonymous type, instead I’m using MyClass). As the return type is List<MyClass>, I had to call the ToList()method. Of course it is possible to remove ToList() by changing the return type to IEnumerable<MyClass>. Note also that the intellisense is now telling you that name is a property of the MyClass type!
Of course making use of self defined types instead of anonymous types can be very useful. Just to mention 2 situations I found myself in the last weeks. The first problem I got by using anonymous type, was when I tried to bind the result of a LINQ to SQL query expression to a datagrid: it was not possible to change data. The second problem with anonymous type is the scope of an anonymous object: I wasn’t able to pass the type to another method (you can’t define a parameter of type var; var must always be followed by an initialization expression!). Thanks to a self defined return type you can solve all these problems.
Hope this helps,
PingBack from http://msdnrss.thecoderblogs.com/2008/01/25/using-your-own-defined-type-in-a-linq-query-expression/