from c in Contact
select new { c.FirstName, c.LastName, c.EmailAddress};
Anonymous types derive directly from the Object class as reference types. A compiler assigns the
anonymous type a name, and it is not available at the source code level. Anonymous types are no
different from any other type as far as the CLR (Common Language Runtime) is concerned.
This might seem a little confusing, but it will all be cleared up when LINQ and query expressions are
discussed in detail in Chapter 3.
Visual Basic
Anonymous types, new to Visual Basic for 2008, let you create objects without needing to write a class
definition for the data type. In Visual Basic 2008, the compiler generates the class for you. A LINQ query
expression uses anonymous types to join or combine columns from a query.
The great thing about anonymous types is that they let you write queries that return any number of
columns in any order. The compiler has the responsibility of creating the data types that correspond to
the specified properties (columns).
Here??™s the previous anonymous type example in Visual Basic .
Pages:
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70