click below
click below
Normal Size Small Size show me how
Chapter 9
Introduction to LINQ and the List Collection
Term | Definition |
---|---|
List | Similar to an an array but provides additional functionality, such as dynamic resizing - a List can increase its size when items are added to it and decrease its size when items are removed |
Database | An organized collection of data. Large amounts of data that need to persist beyond an app's execution are typically stored in a database |
Database Management System(DBMS) | Provides mechanisms for storing, organizing, retrieving, and modifying data in the database. |
SQL | Pronounced "sequel". Is an international standard used to perform queries(to request information that satisfies given criteria) and to manipulate data |
LINQ(Language Integrated Query) | Allows you to write query expressions, similar to SQL queries, that retrieve information from a variety of data sources, not just databases. |
Filtering | Using LINQ to Objects to query arrays and Lists, selecting elements that satisfy a set of conditions |
LINQ Provider | 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. LINQ queries may be used in many different contexts because of providers |
Declaritive Programming | As opposed to imperative programming(which we've been doing so far) you specify the actual steps to perform a task. An example is how LINQ specifies the conditions that selected elements must satisfy |
Using LINQ to Objects | To use LINQ to Objects, you must import the System.Linq namespace |
1. from Clause | A LINQ query begins with a from clause, which specifies a range variable(value) and the data source to query(values). The range variable represents each item in the data source(one at a time), much like the control variable in a foreach statement. |
2. from Clause | Ex: var filtered = ... from value in values... where value > 4... select value; |
Implicitly Typed Local Variables | Enables the compiler to infer a local variable's type based on the context in which it is used |
var Keyword | Is used in place of the variable's type when declaring the variable. You can declare a local variable and let the compiler infer the variable's type based on the variable's initalizer. Ex: var x = 7; |
1. where Clause | If the condition in the where clause evaluates to true, the element is selected - it's included in the results. |
2. where Clause | Ex: var filtered = ... from value in values... where value > 4... select value; |
Predicate | An expression that takes an element of a collection and returns true or false by testing a condition on that element |
1. select Clause | Foe each item in the data source, the select clause determines what value appears in the result. A LINQ query typically ends with a select clause |
2. select Clause | Ex: var filtered = ... from value in values... where value > 4... select value; |
orderby Clause | Sorts the query results in ascending order... Ex: var filtered = ... from value in values... orderby value... select value; |
descending Modifier to the orderby Clause | The descending modifier in the orderby clause sorts the results in descending order... Ex: var filtered = ... from value in values... orderby value... select value;... An ascending modifier exists but isn't normally used beccause it's the default |
1. More On Implicitly Typed Local Variables | Implicitly typed local variables can also be used to initialize arrays without explicitly giving their type. The following statement creates an array of int values... var array = new[] {32, 27, 64, 18, 95, 14}... |
2. More On Implicitly Typed Local Variables | Note that there are no square brackets on the left side of the assignment operator, and that new[] is used to specify that the variable is an array |
IEnumberable<T> | Is an interface. foreach statements iterate over any IEnumerable<T> object, which is what most LINQ queries return. Describes functionality of objects that can be iterated over and offers members to access each element. Arrays are IEnumberable<T> objects |
Interfaces | Define and standardize the ways in which people and systems can interact with one another |
Query Result's Any Method | Returns true if there's at least one element, and false if there are no elements. Ex: nameSorted.Any() |
Query Result's First Method | Returns the first element in the result. You should check that the query result is not empty before calling the First method. Ex: nameSorted.First() |
Extension Method | Can be used to enhance a class's capabilities without modifying the class's definition. The LINQ extension methods can be used as if they were methods of IEnumerable<T>. The methods First and Any are extension methods |
Count() Extension Method | Returns the number of elements in the results |
Distinct Extension Method | Removes duplicate elements, causing all elements in the result to be unique. Ex: lastNames.Distinct() |
Projection | When a query performs a transformation on the data |
Collections | Several classes that the .NET Framework Class Library provides which are used to store groups of related objects. These classes provide efficient methods that organize, store, and retrieve data without requiring knowledge of how the data is being stored |
Collection Class List <T> | The T is a placeholder - when declaring a new List, replace it with the type of elements that you want the List to hold. For example, List<int> list1; declares list1 as a List collection that can store only int value. |
Generic Classes | Classes with a placeholder that can be used with any type. Ex: List<T> |
1. Some Methods and Properties of Class List<T> | 1. Method or Property[Add]..Description[Adds an element to the end of the List]... 2. Method or Property[Capacity]..Description[Property that gets and set the number of elements a List can store without resizing]... |
2. Some Methods and Properties of Class List<T> | 3. Method or Property[Clear]..Description[Removes all the elements from the List]... 4. Method or Property[Contains]..Description[Returns true if the List contains the specified element and false otherwise]... |
3. Some Methods and Properties of Class List<T> | 5. Method or Property[Count]..Description[Property that returns the number of elements stored in the List]... 6. Method or Property[IndexOf]..Description[Returns the index of the first occurrence of the specified value in the List]... |
4. Some Methods and Properties of Class List<T> | 7. Method or Property[Insert]..Description[Inserts the element at the specified index]... 8. Method or Property[Remove]..Description[Removes the first occurrence of the specified value]... |
5. Some Methods and Properties of Class List<T> | 9. Method or Property[RemoveAt]..Description[Removes the element at the specified index]... 10. Method or Property[RemoveRange]..Description[Removes a specified number of elements starting at a specified index]... |
6. Some Methods and Properties of Class List<T> | 11. Method or Property[Sort]..Description[Sorts the List]... 12. Method or Property[TrimExcess]..Description[Sets the Capacity of the List to the number of elements the List currently contains(count)] |
Add Method | Appends its argument to the end of the List. Ex: items.Add("red"); |
Insert Method | Inserts a new element at the specified position. Ex: items.Insert(0, "yellow"); |
Count Property | Returns the number of elements currently in the List. Ex: items.Count; |
Remove Method | Is used to remove the first element with a specific value. If no such element is in the List, Remove does nothing. Ex: items.Remove("yellow"); |
RemoveAt Method | Removes the element at a specified index. Ex: items.RemoveAt(1); |
Contains Method | Returns true if the element it found in the List, and false otherwise. Ex: items.Contains("red"); |
Capacity Property | Indicates how many items the List can hold without having to grow |
1. let Clause | let declares a new range variable to which you assign result of an expression that operates on the query's original range variable. Useful if you need to store a temporary result for later use in the LINQ query. |
2. let Clause | Ex: var startsWithR = ... from item in items... let uppercaseString = item.ToUpper()... where uppercaseStrin.StartsWith("R")... orderby uppercaseString... select uppercaseString; |
LINQ's Deferred Execution | The query execute only when you access the results - such as iterating over them of using the Count method - not when you define the query. This allows you to create a query one and execute it many times |
Collection Initalizers | Provide a convenient syntax(similar to array initializers) for initializing a collection. Ex: List<string> items = ... new List<string> {"aQua", "RusT", "yElLow", "rEd"}; |
1. StartsWith Method | Is used to filter strings starting with a specified character of series of characters. |
2. StartsWith Method | Ex: var startsWithR = ... from item in items... let uppercaseString = item.ToUpper()... where uppercaseStrin.StartsWith("R")... orderby uppercaseString... select uppercaseString; |