13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introduc<strong>in</strong>g LINQ277the extension methods of Enumerable but do it without query expressions, does thatcount? What about pla<strong>in</strong> lambda expressions without any query<strong>in</strong>g go<strong>in</strong>g on?As a concrete example of this question, let’s consider four slightly different ways ofachiev<strong>in</strong>g the same goal. Suppose we have a List (where Person has propertiesName and Age as <strong>in</strong> chapter 8) and we wish to apply a transformation so that we geta sequence of str<strong>in</strong>gs, just the names of the people <strong>in</strong> the list. Us<strong>in</strong>g lambda expressions<strong>in</strong> preference to anonymous methods, we can still use the standard ConvertAll methodof List, as follows:var names = people.ConvertAll(p => p.Name);Alternatively we can use the Select method of the Enumerable class, because Listimplements IEnumerable. The result of this will be an IEnumerablerather than a List, but <strong>in</strong> many cases that’s f<strong>in</strong>e. The code becomesvar names = Enumerable.Select(people, p => p.Name);Know<strong>in</strong>g that Select is an extension method, we can simplify th<strong>in</strong>gs a little bit:var names = people.Select(p => p.Name);F<strong>in</strong>ally, we could use a query expression:var names = from p <strong>in</strong> people select p.Name;Four one-l<strong>in</strong>ers, all of which accomplish much the same goal. 2 Which of them count asLINQ? My personal answer is that the first isn’t really us<strong>in</strong>g LINQ (even though it uses alambda expression), but the rest are LINQ-based solutions. The second form certa<strong>in</strong>lyisn’t idiomatic, but the last two are both perfectly respectable LINQ ways of achiev<strong>in</strong>gthe same aim, and <strong>in</strong> fact all of the last three samples compile to the same IL.In the end, there are no bonus po<strong>in</strong>ts available for us<strong>in</strong>g LINQ, so the question ismoot. However, it’s worth be<strong>in</strong>g aware that when a fellow developer says they’re us<strong>in</strong>gLINQ to solve a particular problem, that statement could have a variety of mean<strong>in</strong>gs.The long and the short of it is that LINQ is a collection of technologies, <strong>in</strong>clud<strong>in</strong>gthe language features of <strong>C#</strong> 3 (and VB9) along with the framework libraries provided aspart of .NET 3.5. If your project targets .NET 3.5, you can use LINQ as much or as littleas you like. You can use just the support for <strong>in</strong>-memory query<strong>in</strong>g, otherwise known asLINQ to Objects, or providers that target XML documents, relational databases, or otherdata sources. The only provider we’ll use <strong>in</strong> this chapter is LINQ to Objects, and <strong>in</strong> thenext chapter we’ll see how the same concepts apply to the other providers.There are a few concepts that are vital to LINQ. We’ve seen them tangentially <strong>in</strong>chapter 10, but let’s look at them a little more closely.11.1.2 Fundamental concepts <strong>in</strong> LINQMost of this chapter is dedicated to exactly what the <strong>C#</strong> 3 compiler does with queryexpressions, but it won’t make much sense until we have a better understand<strong>in</strong>g of the2There’s actually a big difference between ConvertAll and Select, as we’ll see <strong>in</strong> a m<strong>in</strong>ute, but <strong>in</strong> many caseseither could be used.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!