11.07.2015 Views

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

SHOW MORE
SHOW LESS

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

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

426CHAPTER 15 ■ LEARNING ABOUT LINQThe ideas of LINQ that include <strong>from</strong>, where, and select are not lost; they just have not beenused. The <strong>from</strong> part is the _tickets variable itself. The where part is the method Where(), andthe select part is a default selection of the currently selected node.To specify an action with Where(), you use a lambda expression, which has two parameters:the object and the index of the object. The lambda expression expects that you return a Booleanvalue indicating whether the ticket item should be added <strong>to</strong> a returned list.When you use the list methods associated with the type, you are using a different functionalitythan LINQ in the abstract sense. LINQ is a syntax that wraps SQL-like text. LINQ ismuch easier <strong>to</strong> understand and program. Using the methods gives you more flexibility, butthey also are more complicated <strong>to</strong> write.For example, if you wanted <strong>to</strong> find the frequency of two numbers in a list, you could usethis code:int FrequencyOfTwoNumbersList(int number1ToSearch, int number2ToSearch) {var query = _tickets.Where((ticket, index) =>ticket.Numbers[0] == number1ToSearch|| ticket.Numbers[1] == number1ToSearch|| ticket.Numbers[2] == number1ToSearch|| ticket.Numbers[3] == number1ToSearch|| ticket.Numbers[4] == number1ToSearch|| ticket.Numbers[5] == number1ToSearch).Where((ticket, index) =>ticket.Numbers[0] == number2ToSearch|| ticket.Numbers[1] == number2ToSearch|| ticket.Numbers[2] == number2ToSearch|| ticket.Numbers[3] == number2ToSearch|| ticket.Numbers[4] == number2ToSearch|| ticket.Numbers[5] == number2ToSearch);}return query.Count();In the code, the bolded line demonstrates how the output of one method can serve as theinput for another method. This chaining of methods works because the list method returns otherlists. Thus, you could add multiple criteria by concatenating multiple Where() method calls.The methods are used <strong>to</strong> filter or manipulate the set where the details of the method areprovided by a lambda expression. Table 15-1 briefly describes some of the useful methods thatyou can use <strong>to</strong> filter and manipulate a list. The best way <strong>to</strong> learn about all of the methods is <strong>to</strong>use Visual <strong>C#</strong> Express, declare a list, and use IntelliSense <strong>to</strong> discover the different methodsavailable. Also, see http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx for manyexamples that demonstrate the various list-manipulation methods.

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

Saved successfully!

Ooh no, something went wrong!