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.

Simple beg<strong>in</strong>n<strong>in</strong>gs: select<strong>in</strong>g elements283As you can see, we’re not record<strong>in</strong>g an awful lot of data. In particular, there’s no realhistory to the defects, but there’s enough here to let us demonstrate the queryexpression features of <strong>C#</strong> 3. For the purposes of this chapter, all the data is stored <strong>in</strong>memory. We have a class named SampleData with properties AllDefects, AllUsers,AllProjects, and AllSubscriptions, which each return an appropriate type ofIEnumerable. The Start and End properties return DateTime <strong>in</strong>stances for thestart and end of August respectively, and there are nested classes Users andProjects with<strong>in</strong> SampleData to provide easy access to a particular user or project.The one type that may not be immediately obvious is NotificationSubscription:the idea beh<strong>in</strong>d this is to send an email to the specified address every time a defectis created or changed <strong>in</strong> the relevant project.There are 41 defects <strong>in</strong> the sample data, created us<strong>in</strong>g <strong>C#</strong> 3 object <strong>in</strong>itializers. All ofthe code is available on the book’s website, but I won’t <strong>in</strong>clude the sample data itself<strong>in</strong> this chapter.Now that the prelim<strong>in</strong>aries are dealt with, let’s get crack<strong>in</strong>g with some queries!11.2 Simple beg<strong>in</strong>n<strong>in</strong>gs: select<strong>in</strong>g elementsHav<strong>in</strong>g brought up some general LINQ concepts beforehand, I’ll <strong>in</strong>troduce the conceptsthat are specific to <strong>C#</strong> 3 as they arise <strong>in</strong> the course of the rest of the chapter. We’rego<strong>in</strong>g to start with a simple query (even simpler than the ones we’ve seen so far <strong>in</strong> thechapter) and work up to some quite complicated ones, not only build<strong>in</strong>g up your expertiseof what the <strong>C#</strong> 3 compiler is do<strong>in</strong>g, but also teach<strong>in</strong>g you how to read LINQ code.All of our examples will follow the pattern of def<strong>in</strong><strong>in</strong>g a query, and then pr<strong>in</strong>t<strong>in</strong>gthe results to the console. We’re not <strong>in</strong>terested <strong>in</strong> b<strong>in</strong>d<strong>in</strong>g queries to data grids or anyth<strong>in</strong>glike that—it’s all important, but not directly relevant to learn<strong>in</strong>g <strong>C#</strong> 3.We can use a simple expression that just pr<strong>in</strong>ts out all our users as the start<strong>in</strong>gpo<strong>in</strong>t for exam<strong>in</strong><strong>in</strong>g what the compiler is do<strong>in</strong>g beh<strong>in</strong>d the scenes and learn<strong>in</strong>g aboutrange variables.11.2.1 Start<strong>in</strong>g with a source and end<strong>in</strong>g with a selectionEvery query expression <strong>in</strong> <strong>C#</strong> 3 starts off <strong>in</strong> the same way—stat<strong>in</strong>g the source of asequence of data:from element <strong>in</strong> sourceThe element part is just an identifier, with an optional type name before it. Most ofthe time you won’t need the type name, and we won’t have one for our first example.Lots of different th<strong>in</strong>gs can happen after that first clause, but sooner or later youalways end with a select clause or a group clause. We’ll start off with a select clauseto keep th<strong>in</strong>gs nice and simple. The syntax for a select clause is also easy:select expressionThe select clause is known as a projection. Comb<strong>in</strong><strong>in</strong>g the two together and us<strong>in</strong>g themost trivial expression we can th<strong>in</strong>k of gives a simple (and practically useless) query,as shown <strong>in</strong> list<strong>in</strong>g 11.1.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!