Introduction

Chapter 8 introduced arrays, simple data structures used to store items of a specific type. Although commonly used, arrays have limited capabilities. For instance, you must specify an array’s size when you create it. If, at execution time, you wish to modify that size, you must do so manually by creating a new array and copying elements into it or by using class Array’s Resize method, which performs those tasks for you. In this chapter, we introduce the .NET Framework’s List collection class, which offers greater capabilities than traditional arrays. A List is similar to an array but provides additional functionality, such as dynamic resizing, a List can increase its size when items are added to it. We use the List collection to implement several data manipulations similar to those in the preceding chapter. List and the .NET Framework’s other collections are reusable, reliable, powerful and efficient and have been carefully designed and tested to ensure correctness and good performance.

Large amounts of data that need to persist beyond an app’s execution are typically stored in a database, an organized collection of data. A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data in a database. A language called SQL (Structured Query Language), pronounced “sequel”, is the international standard used to perform queries and to manipulate data in relational databases. These organize data in tables that maintain relationships between pieces of data stored in each table, a key goal is to eliminate duplicate data. For years, programs accessing a relational database passed SQL queries to the database management system, then processed the returned results. This chapter introduces C#’s LINQ (Language Integrated Query) capabilities. LINQ allows you to write query expressions, similar to SQL queries, that retrieve information from a variety of data sources, not just databases. In this chapter, we use LINQ to Objects to manipulate objects in memory, such as arrays and Lists.


LINQ Providers

The syntax of LINQ is built into C#, but LINQ queries may be used in many contexts via libraries known as providers. A LINQ provider is a set of classes that implement LINQ operations and enable programs to interact with data sources to perform tasks such as sorting, grouping and filtering elements. Many LINQ providers are more specialized, allowing you to interact with a specific website or data format.


LINQ Query Syntax vs. Method-Call Syntax

There are two LINQ approaches—one uses a SQL-like syntax and the other uses method call syntax. This chapter shows the simpler SQL-like syntax. In a later chapter, we’ll show the method-call syntax, introducing the notions of delegates and lambdas—mechanisms that enable you to pass methods to other methods to help them perform their tasks.