Basics of LINQ (III) and basics of linq

Source: Internet
Author: User

Basics of LINQ (III) and basics of linq

I. Parallel LINQ
The ParallelEnumerable class in the System. Linq namespace can be used to break down the query and distribute it across multiple threads.
Although the Enumerable class defines extension methods for the IEnumerable <T> interface, most extension methods of the ParallelEnumerable class are extensions of the ParallerQuery <TSource> class. For example, the AsParallel () method extends the IEnumerable <T> interface and returns the ParallelQuery <T> class. Therefore, normal collection classes can be queried in parallel.

1. Parallel Query
The following demonstrates Parallel LINQ (Parallel LINQ, PLINQ ):

// Fill in a large int set with a random value // Enumerable. Range (0, arraySize) to generate an empty sequence of integers within the specified Range. // Select (x => r. next (140), fill in the set static IEnumerable <int> SampleData () {const int arraySize = 140; var r = new Random (); return Enumerable. range (0, arraySize ). select (x => r. next (140 )). toList ();} static void IntroParallel () {var data = SampleData (); var watch = new Stopwatch (); // non-parallel LINQ watch. start (); var q1 = (from x in data where Math. log (x) <4 select x ). average (); watch. stop (); Console. writeLine ("sync {0}, result: {1}", watch. elapsedMilliseconds, q1); watch. reset (); // use data. asParallel () for parallel LINQ watch. start (); var q2 = (from x in data. asParallel () where Math. log (x) <4 select x ). average (); watch. stop (); Console. writeLine ("async {0}, result: {1}", watch. elapsedMilliseconds, q2 );}

Output;
    
It is found that the time used for parallel query is small, and the CPU usage reaches 100% during parallel query.

The compiler modifies the syntax to call the AsParallel, Where (), Select (), Average () methods, just like the LINQ query in the basics (2) (http://www.cnblogs.com/afei-24/p/6845551.html:
Var q2 = data. AsParallel (). Where (x => Math. Log (x) <4). Select (x => x). Average ();
The AsParallel () method is defined using the ParallerEnumerable class to extend the IEnumerable <T> interface, so you can call it on a simple array. The AsParallel () method returns ParallerQuery <T>. Because of the returned type, the Where () method selected by the compiler is ParallerEnumerable. Where () instead of Enumerable. Where ().
For the PrarllelEnumerable class, the query is partitioned so that multiple threads can process the query at the same time. A set can be divided into multiple parts, each of which is processed by different threads. After completing the partitioning, You need to merge to obtain the sum of all parts.

2. Partition Machine
The AsParallel () method not only extends the IEnumerable <T> interface, but also extends the Partitioner class. It can affect the partitions to be created.
The Partitioner class is defined using the System, Collection. Concurrent namespace and has different variants. The Create () method accepts arrays or objects that implement the IList <T> class and Boolean parameters, and returns a different Partitioner type. The Create () method has multiple overloaded versions.
Var q2 = (from x in Partitioner. Create (data). AsParallel ()
Where Math. Log (x) <4
Select x). Average ();

You can also call the WithExecutionMode () and WithDegreeOfParallelism () Methods on the AsParallel () method to influence the parallel mechanism. The WithExecutionMode () method can pass a Default value or ForceParallelism value of ParallelExecutionMode. By default, parallel LINQ avoids the use of parallel mechanisms with high system overhead. The WithDegreeOfParallelism () method can pass an integer to specify the maximum number of tasks to be run in parallel. This method is useful if the query should not use all CPUs.


  3. Cancel
To cancel a long-running query, you can add the WithCancellation () method to the query and pass a CancellationToken as the parameter. The CancellationToken token is created from the CancellationTokenSource class.
For example, the following query runs in a separate thread. If the query is canceled, an OperationCanceledException exception is caught in the thread. In the main thread, you can call the CancellationTokenSource class's Cancle () method to cancel the task.

var data = SampleData();          var watch = new Stopwatch();          watch.Start();          Console.WriteLine("filled array");          var sum1 = (from x in data                      where Math.Log(x) < 4                      select x).Average();          Console.WriteLine("sync result {0}", sum1);          var cts = new CancellationTokenSource();                    Task.Factory.StartNew(() =>            {              try              {                var res = (from x in data.AsParallel().WithCancellation(cts.Token)                           where Math.Log(x) < 4                           select x).Average();                Console.WriteLine("query finished, result: {0}", res);              }              catch (OperationCanceledException ex)              {                Console.WriteLine(ex.Message);              }            });          watch.Stop();          Console.WriteLine("async {0}, result: {1}", watch.ElapsedMilliseconds, "res");          Console.WriteLine("query started");          Console.Write("cancel? ");          string input = Console.ReadLine();          if (input.ToLower().Equals("y"))          {              cts.Cancel();              Console.WriteLine("sent a cancel");          }          Console.WriteLine("press return to exit");          Console.ReadLine();


  Ii. Expression Tree
In LINQ To Object, the extension method must use a delegate type as a parameter, so that the lambda expression can be assigned To the parameter. Lambda expressions can also be granted with parameters of the Expression <T> type. The C # compiler defines different behaviors for lambda expressions based on the type. If the type is Expression <T>, the compiler creates an Expression tree from the lambda Expression and stores it in the dataset. In this way, you can analyze the expression tree during running and optimize it to query the data source.
Var racers = from r in Formula1.GetChampions ()
Where r. Wins> 15 & (r. Country = "Brazil" | r. Country = "Austria ")
Select r;

The query expression uses the extension method Where () and Select. The Enumerable class defines the Where () method and uses the delegate type Func <T, bool> as the parameter predicate:
Public static IEnumerable <TSource> Where <TSource> (this IEnumerable <TSource> source, Func <TSource, bool> predicate );
In this way, the lambda expression can be assigned to the delegate predicate.

In addition to delegation, the compiler also places the Expression Tree in the Assembly. The expression tree can be read during running. The Expression Tree is constructed from the class derived from the abstract base class Expression. Expression and Expression <T> are different. Expressions inherited from the Expression class include BinaryExpression, ConstantExpression, and InvocationExpression. The compiler creates an Expression Tree from a lambda expression.
For example, lambda expression r. country = "Brazil" uses ParameterExpression, MemberExpression, ConstantExpression, and MethodCallExpression to create an Expression Tree, store the tree in the Assembly, and then use the tree during runtime, create an optimized query for the underlying data source:

// Display Expression Tree in the console. Pass an Expression object, and write some information about the Expression to the private static void DisplayTree (int indent, string message, expression Expression) {string output = String. format ("{0} {1 }! NodeType: {2}; Expr: {3 }","". padLeft (indent, '>'), message, expression. nodeType, expression); indent ++; switch (expression. nodeType) {case ExpressionType. lambda: Console. writeLine (output); LambdaExpression lambdaExpr = (LambdaExpression) expression; foreach (var parameter in lambdaExpr. parameters) {DisplayTree (indent, "Parameter", parameter);} DisplayTree (indent, "Body", lambdaExpr. body); brea K; case ExpressionType. constant: ConstantExpression constExpr = (ConstantExpression) expression; Console. writeLine ("{0} Const Value: {1}", output, constExpr. value); break; case ExpressionType. parameter: ParameterExpression paramExpr = (ParameterExpression) expression; Console. writeLine ("{0} Param Type: {1}", output, paramExpr. type. name); break; case ExpressionType. equal: case ExpressionType. andAlso: Case ExpressionType. GreaterThan: BinaryExpression binExpr = (BinaryExpression) expression; if (binExpr. Method! = Null) {Console. writeLine ("{0} Method: {1}", output, binExpr. method. name);} else {Console. writeLine (output);} DisplayTree (indent, "Left", binExpr. left); DisplayTree (indent, "Right", binExpr. right); break; case ExpressionType. memberAccess: MemberExpression memberExpr = (MemberExpression) expression; Console. writeLine ("{0} Member Name: {1}, Type: {2}", output, memberExpr. member. name, memberExpr. type. name); DisplayTree (indent, "Member Expr", memberExpr. expression); break; default: Console. writeLine (); Console. writeLine ("{0} {1}", expression. nodeType, expression. type. name); break ;}} static void Main () {Expression <Func <Racer, bool> expression = r => r. country = "Brazil" & r. wins> 6; DisplayTree (0, "Lambda", expression );}

Output:
  

An example of Expression <T> type is the client provider of ADO. net ef and WCF data service. These techniques use the Expression <T> parameter to define the extension method. In this way, the LINQ provider accessing the database can read expressions, create a query optimized during running, and obtain data from the database.
The use of the expression tree will be introduced separately later.

  3. LINQ providers
. NET contains several LINQ providers. The LINQ provider implements standard query operators for specific data sources. The LINQ provider may implement more extension methods than the ones defined by LINQ, but at least implement standard operators. LINQ To XML implements some methods specifically used for XML, which will be detailed later.
The implementation scheme of the LINQ provider is based on the namespace and the type of the first parameter. The namespace of the class implementing the extension method must be open; otherwise, the extension method is not in the scope. Parameters of the Where () method defined in LINQ to Objects are different from those of the Where () method defined in LINQ To Entities:
The Where () method defined in LINQ to Objects:
Public static IEnumerable <TSource> Where <TSource> (this IEnumerable <TSource> source, Func <TSource, bool> predicate );
The Where () method defined in LINQ To Entities:
Public static IQueryable <TSource> Where <TSource> (this IQueryable <TSource> source, Expression <Func <TSource, bool> predicate );
Both of these classes are implemented in the. Core program of System. Linq. Whether using Func <TSource, bool> to pass parameters or using Expression <Func <TSource, bool> to pass parameters, lambda expressions are all the same. Only the compiler has different behaviors. It is selected based on the source parameter. The compiler selects the most matching method based on its parameters. In ADO. the CreateQuery () method of the ObjectContext class defined in net ef returns an ObjectQuery <T> object that implements the IQueryable <TSource> interface. Therefore, EF uses the Where () method of the Querable class () method.

Related Article

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.