20.C#LINQ Basics and Simple use (11 chapters 11.1-11.2)

Source: Internet
Author: User



Finally see the 11th chapter, although have seen before, but not too carefully, in the work also occasionally will use, but do not understand the principle, that now speak of LINQ, do a book worm ~ ~



Let's start by understanding the three main points of LINQ:


    1. LINQ cannot convert very complex query expressions into one line of code
    2. Using LINQ does not mean that you no longer need to use SQL
    3. LINQ does not magically make you an architect genius,


The sequence is the basis of LINQ, and when you see a query expression, you should think of the sequence it involves: At first there is always at least one sequence, and usually the intermediate process is converted to another sequence, or it may be connected to another sequence.


 
 
 1 class Car
 2 {
 3     public string Owner { get; set; }
 4     public double Mileage { get; set; }
 5 }
 6 
 7 class Program
 8 {
 9     static void Main(string[] args)
10     {
11         var list = new List<Car> {
12             new Car() { Owner="a",Mileage=100},
13             new Car() { Owner="b",Mileage=200},
14             new Car() { Owner="c",Mileage=300},
15             new Car() { Owner="d",Mileage=400},
16             new Car() { Owner="e",Mileage=500},
17             new Car() { Owner="f",Mileage=600}
18         };
19 
20         var result = from e in list
21                      where e.Mileage > 200
22                      select e;
23 
24         result.ToList().ForEach(x => Console.WriteLine(x.Owner));
25 
26         Console.ReadKey();
27 
28     }
29 }


The expression above: find a car with a mileage greater than 200 from a sequence of cars . The next line of code uses a lambda expression to print the filtered owner name.


    • Deferred execution and stream processing


  when the query expression in the above is created, the data is not processed immediately, and the original car sequence is not accessed, but it is generated in memory as a representation of the query, a feature called deferred execution . As below, in result. The operation of the sequence is only started when ToList () is executed.


    • Projection


A statement in the Select expression format, called a projection.


1 ). Select (e = e);


query expressions translate the compiler into the above code, and you can see that they are made up of extension methods and lambda expressions. At any time, the parameters (mostly) are delegate types, and the compiler will use a lambda expression as an argument and try to find a method with the appropriate signature . And look at our expressions.


 
1 var result = from e in list
2              where e.Mileage > 200
3              select e;


The From, where, in, select are the query expression context keywords, E is the range variable, the list is the data source sequence, and select E is the return projection. Look again at an example that returns a projection.


 
1 var result1 = from e in list
2               where e.Mileage > 200
3               select e.Owner;


Here the Select E.owner, which returns the name of each owner, then the RESULT1 type is ienumerable<string>


    • Cast method and OfType method
 
1 class Bigtruck :Car
 2 {
 3     public double Volume { get; set; }
 4 
 5 }
 6 
 7 var list0 = new List<Car> {
 8     new Car() { Owner="a",Mileage=100},
 9     new Bigtruck() { Owner="b",Mileage=200,Volume=100.1},
10     new Bigtruck() { Owner="c",Mileage=300,Volume=100.2},
11     new Bigtruck() { Owner="d",Mileage=400,Volume=100.3},
12     new Bigtruck() { Owner="e",Mileage=500,Volume=100.4},
13     new Bigtruck() { Owner="f",Mileage=600,Volume=100.5}
14 };
15 
16 var castList = list0.Cast<Car>();
17 var oftypeList = list0.OfType<Bigtruck>();
18 
19 Console.WriteLine(castList.Count());  //6
20 Console.WriteLine(oftypeList.Count());  //5


  using cast<t> () converts the elements in the list to the T type, encounters an error with an element that cannot be converted, and oftype<t> () attempts to convert each element to the T type, skipping the element that cannot be converted . When you explicitly declare the use of a range variable, the cast () method is called at translation time. Such as


 
1 var result2 = from Car e in list
2               select e;
3 result2 = list.Cast<Car>().Select(e => e);


The expression is translated into the third line of code.


    • Concept
    1. LINQ is based on a data list and is streamed wherever possible
    2. Creates a query expression that does not execute immediately, and most operations defer execution
    3. C#3 's query expressions include a preprocessing phase that transforms an expression into plain C # code, followed by common operations such as type inference, overloading, and lambda expressions to properly compile the converted Code
    4. Variables declared in query expressions: They are just scope variables through which you can consistently reference data within a query expression


Please treatise.






20.C#LINQ Basics and Simple use (11 chapters 11.1-11.2)


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.