LINQ overview, syntax, and examples

Source: Internet
Author: User
Overview

These seven foundational principles state that LINQ is:

ØIntegrated: LINQ is a short RST-class citizen. net ages such as C # and VB and as such is fully type-checked. inside Visual Studio it is syntax-highlighted and intelliisense-aware.

ØUnitive:LINQ provides a single syntax for querying multiple data sources, including relational data found in a SQL database, XML data, and the objects in a program.

ØExtensible:LINQ can be adapted to work with multiple programming ages and to query multiple data sources. LINQ to XML, LINQ to SQL, and LINQ to objects are only three possible forms of LINQ. developers can extend the language to query almost any arbitrary data source, such as a role le system, Web Service, or network protocol.

ØDeclarative:A linq developer tells the compiler what to do, without focusing on how to perform a task or in what order tasks must be completed MED.

ØHierarchical:LINQ provides a rich, object-oriented view of data. A more rigorous or mathematical view of this same theme wocould focus on LINQ's capability to generate and manipulate graphs.

ØComposable:The results of one query can be used by a second query, and one query can be a subclause of another query. in our cases, this can be done without forcing the execution of any one query until the developer wants that execution to take place. thus, you can write three separate but related queries. LINQ automatically notes the connections between them and combines them into a single, EF extends cient query that executes only once. this allows you to "divide and conquer" by breaking up the logic of your query just as you divide the logic of your program into SS multiple classes and methods.

ØTransformative:The results of a LINQ query against one data source can be transformed into a second data source. For instance, a query against a SQL database can produce an XML role le as output.

[Essential linq isbn: 978-0-321-56416-0 chapter 3rd describes these features in detail]

Syntax Language extension

To support LINQ, the Extended Language features are as follows:

The implicit local variable object and the lambda expression in combination with the extension method of the initializer

MS-help: // Ms. VSCC. v90/ms. msdnqtr. v90.chs/dv_csref/html/57e3ba27-9a82-4067-aca7-5ca446b7bf93.htm

A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create a delegate or expression directory tree.

The Lambda expression on the right is called a "Lambda expression ". Lambda expressions are widely used to construct the expression directory tree. Lambda expressions return the results of expressions in the following basic form:

(Input parameters) => Expression

Lambda statements are similar to lambda expressions, but they are enclosed in braces:

(Input parameters) =>{ statement ;}

Func <(of <(T, tresult>)

Examples of Extended features above the anonymous type

Using system;
Using system. Collections. Generic;
Using system. diagnostics;
Using system. LINQ;
Using system. text;
Internal static class processextension
{
Public static long totalmemory (this ienumerable <processdata> processes)
{
// Extended method extension methods
Long result = 0;
Foreach (VAR process in processes)
{
Result + = process. memory;
}
Return result;
}
}
Internal class processdata
{
Public int ID {Get; set ;}
Public long memory {Get; set ;}
Public string name {Get; set ;}
}
Internal class syntax
{
Public void run ()
{
// Lambda expressions
Displayprocesses (process => process. workingset64> = 20*1024*1024 );
VaR list = getsequence ();
VaR query = from num in list
Where num <3
Select num;
// Delayed execution
Foreach (VAR item in query)
{
Console. writeline (item );
}
}
Public ienumerable <int> getsequence ()
{
// The compiler can automatically speculate on the Type
// Use reflector to view the generated Code Situation
VaR length = 3;
// The Compiler generates a secondary structure similar to the state machine to enable movenext to access yield return.
For (INT I = 1; I <= length; I ++)
{
Yield return I;
}
}
Private Static void displayprocesses (func <process, bool> match)
{
// Implicit local variables
VaR processes = new list <processdata> ();
Foreach (VAR process in process. getprocesses ())
{
If (MATCH (process ))
{
// Object and set Initiator
Processes. Add (New processdata
{
Id = process. ID,
Name = process. processname,
Memory = process. workingset64
});
}
}
// Extended method extension methods
Console. writeline ("total memory: {0} MB", processes. totalmemory ()/1024/1024 );
VaR top2memory = processes. orderbydescending (process => process. Memory)
. Take (2)
. Sum (process => process. Memory)/1024/1024;
Console. writeline ("memory consumed by the two most hungry processes: {0} MB", top2memory );
// Anonymous types
VaR Results = new
{
Totalmemory = processes. totalmemory ()/1024/1024,
Top2memory = top2memory,
Processes = Processes
};

 

It is useful to understand the above concepts!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.