Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

An Interview with Amanda Silver on Language Integrated Query (LINQ)


DDJ: Another thing that strikes me about LINQ is that it's dragging a number of very big features into the language with it. Type inference is a pretty big feature all unto itself. Lamba functions are another. Could you talk a little about what those things are?

AS: There are a couple language features that are the basis for LINQ. The first one is the query expression. The query expression is the From...Where...Select. When the compiler compiles it, it can translate it down to an underlying API call. A lot of the other language features enable the construction of that API. Take, for example, lambda functions, which are basically inline functions. I can create a function definition and pass that as an argument to an API call. That's because the underlying LINQ enabled APIs take lambda functions. For example, when you have From...Where...Select, the Where operator is an underlying method call that takes an expression which defines the filter that you're going to apply to the collection. That filter is received in the form of a lambda expression.

The other features, implicitly typed locals, object initializers, and anonymous types, are all about when you're interacting with the results of the query, how do you interact with it? A really common scenario for query is to project a subset of the members of the thing that you're querying over, into a new collection. For example, let's say that I have an employee class. I might only want to see the employee's name and phone number, project them into a list, and then show them in a list box. The way we do that is with object initializers. We can construct an object, and set properties on the object, in the context of an expression. It's very similar to having an inline With. The With statement lets you specify a variable name, and then inside of the With you specify the properties that you want to work with. Object initializers gives you the same functionality in the context of an expression. Because it's in an expression, that means that you can put it into a Select statement.

In that example, if I'm creating an object on-the-fly that represents the results of a query, I don't want to have to name that type. Anonymous types are a way to create a class that is unnamed. You can access the query results with full Intellisense and full strong typing, but you don't have to name a class, and you don't have to create the members of a class.

DDJ: In other words, the definition of that class doesn't show up in the code.

AS: Exactly.

DDJ: For the developer experience, if they find a variable, and they right-click on it and select "go to definition", what happens? Does it take them to the query?

AS: It's funny that you ask that because that's one of the things that we're designing right now.

The last feature that I didn't cover yet was implicitly typed locals, which is the type inference that you mentioned. I can take a variable and assign it to an expression and the result of the express becomes the types of the variable. For example, I can say "Dim x = 5", and this results in a variable x with a data type of Integer. This is the facility that allows us to have anonymous types. If you didn't have type inference, you'd have to name the type. If you say "Dim x", you would have to specify, with a name, what you are dimensioning x as. With implicitly typed locals, we can still have static types and early binding, but you don't have to name the types. Now you can call the projections that you want with your queries, but you don't have to name those types.

If you look at these changes, this is a pretty fundamental change to Visual Basic. Visual Basic has a huge user community, and we take the community feedback very seriously. Any changes to the language we're pretty conservative about, because changes to the language are in the language for pretty much all time. These ones are very important additions, and so we want customer feedback to make sure we get them right.

The other thing is that we're still trying to decide some elements of what query is going to look like. We've shipped three CTPs, but some things are still up in the air, so we'll take community feedback and requests.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.