• Complain

Albahari Ben - LINQ pocket reference

Here you can read online Albahari Ben - LINQ pocket reference full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. City: Sebastopol, Calif., year: 2008, publisher: O’Reilly Media, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Albahari Ben LINQ pocket reference
  • Book:
    LINQ pocket reference
  • Author:
  • Publisher:
    O’Reilly Media
  • Genre:
  • Year:
    2008
  • City:
    Sebastopol, Calif.
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

LINQ pocket reference: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "LINQ pocket reference" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Albahari Ben: author's other books


Who wrote LINQ pocket reference? Find out the surname, the name of the author of the book and a list of all author's works by series.

LINQ pocket reference — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "LINQ pocket reference" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
LINQ Pocket Reference
Joseph Albahari
Ben Albahari
Beijing Cambridge Farnham Kln Sebastopol Tokyo Special Upgrade Offer If you - photo 1

Beijing Cambridge Farnham Kln Sebastopol Tokyo

Special Upgrade Offer

If you purchased this ebook directly from oreilly.com, you have the following benefits:

  • DRM-free ebooks use your ebooks across devices without restrictions or limitations
  • Multiple formats use on your laptop, tablet, or phone
  • Lifetime access, with free updates
  • Dropbox syncing your files, anywhere

If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.

Please note that upgrade offers are not available from sample content.

Chapter 1. LINQ Pocket Reference

LINQ, or Language Integrated Query, allows you to write structured type-safe queries over local object collections and remote data sources. It is a new feature of C# 3.0 and .NET Framework 3.5.

LINQ lets you query any collection implementing IEnumerable<>, whether an array, list, XML DOM, or remote data source (such as a table in SQL Server). LINQ offers the benefits of both compile-time type checking and dynamic query composition.

The core types that support LINQ are defined in the System.Linq and System.Linq.Expressions namespaces in the System.Core assembly.


Note

The examples in this book mirror the examples in Chapters 810 of C# 3.0 in a Nutshell (OReilly) and are preloaded into an interactive querying tool called LINQPad. You can download LINQPad from http://www.linqpad.net/.


Getting Started

The basic units of data in LINQ are sequences and elements . A sequence is any object that implements the generic IEnumerable interface, and an element is each item in the sequence. In the following example, names is a sequence, and Tom, Dick, and Harry are elements:

string[] names = { "Tom", "Dick", "Harry" };

We call such a sequence a local sequence because it represents a local collection of objects in memory.

A query operator is a method that transforms a sequence. A typical query operator accepts an input sequence and emits a transformed output sequence . In the Enumerable class in System.Linq, there are around 40 query operators, all implemented as static extension methods, called standard query operators .


Note

LINQ also supports sequences that can be dynamically fed from a remote data source such as a SQL Server. These sequences additionally implement the IQueryable<> interface and are supported through a matching set of standard query operators in the Queryable class. For more information, see the upcoming Interpreted Queries section.


A query is an expression that transforms sequences with query operators. The simplest query comprises one input sequence and one operator. For instance, we can apply the Where operator on a simple array to extract those whose length is at least four characters as follows:

string[] names = { "Tom", "Dick", "Harry" };IEnumerable filteredNames =System.Linq.Enumerable.Where (names, n => n.Length >= 4);foreach (string n in filteredNames) Console.Write (n + "|"); // Dick|Harry|

Because the standard query operators are implemented as extension methods, we can call Where directly on names as though it were an instance method:

IEnumerable filteredNames = names.Where (n => n.Length >= 4);

For this to compile, you must import the System.Linq namespace. Heres a complete example:

using System;using System.Linq;class LinqDemo{ static void Main( ) { string[] names = { "Tom", "Dick", "Harry" }; IEnumerable filteredNames =names.Where (n => n.Length >= 4); foreach (string name in filteredNames) Console.Write (name + "|"); }}// RESULT: Dick|Harry|

Note

If you are unfamiliar with C#s lambda expressions, extension methods, or implicit typing, visit www.albahari.com/cs3primer.


We can further shorten our query by implicitly typing filteredNames:

var filteredNames = names.Where (n => n.Length >= 4);

Most query operators accept a lambda expression as an argument. The lambda expression helps guide and shape the query. In our example, the lambda expression is as follows:

n => n.Length >= 4

The input argument corresponds to an input element. In this case, the input argument n represents each name in the array and is of type string. The Where operator requires that the lambda expression return a bool value, which if true, indicates that the element should be included in the output sequence.

In this book, we describe such queries as lambda queries . C# also defines a special syntax for writing queries, called query comprehension syntax . Heres the preceding query expressed in comprehension syntax:

IEnumerable filteredNames = from n in names where n.Contains ("a") select n;

Lambda syntax and comprehension syntax are complementary. In the following sections, we explore each in more detail.

Lambda Queries

Lambda queries are the most flexible and fundamental. In this section, we describe how to chain operators to form more complex queries and introduce several new query operators.

Chaining Query Operators

To build more complex queries, you add additional query operators, creating a chain. For example, the following query extracts all strings containing the letter a , sorts them by length, and then converts the results to uppercase:

string[] names = { "Tom","Dick","Harry","Mary","Jay" };IEnumerable query = names .Where (n => n.Contains ("a")) .OrderBy (n => n.Length) .Select (n => n.ToUpper( ));foreach (string name in query) Console.Write (name + "|");// RESULT: JAY|MARY|HARRY|

Where, OrderBy, and Select are all standard query operators that resolve to extension methods in the Enumerable class.

We already introduced the Where operator, which emits a filtered version of the input sequence. The OrderBy operator emits a sorted version of its input sequence; the Select method emits a sequence where each input element is transformed or projected with a given lambda expression (n.ToUpper(), in this case). Data flows from left to right through the chain of operators, so the data is first filtered, then sorted, then projected.


Note

A query operator never alters the input sequence; instead, it returns a new sequence. This is consistent with the functional programming paradigm, from which LINQ was inspired.


Here are the signatures of each of these extension methods (with the OrderBy signature simplified slightly):

static IEnumerable Where ( this IEnumerable source, Func predicate)static IEnumerable OrderBy ( this IEnumerable source, Func keySelector)static IEnumerable Select ( this IEnumerable source, Func selector)
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «LINQ pocket reference»

Look at similar books to LINQ pocket reference. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «LINQ pocket reference»

Discussion, reviews of the book LINQ pocket reference and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.