20. C # basic and simple use of LINQ (Chapter 11, chapter 11.1-11.2 ),
Finally, I saw chapter 11th. Although I have read it before, it was not very careful and occasionally used in my work. But I don't understand the principles. Now let's talk about LINQ, become a bookworm ~~
First, let's take a look at the three main points of LINQ:
Sequence is the basis of LINQ. When you see a query expression, you should think of the sequence involved in it: At first there is always at least one sequence, generally, it is converted into other sequences during the intermediate process, and may be connected with other sequences.
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 list21 where e.Mileage > 20022 select e;23 24 result.ToList().ForEach(x => Console.WriteLine(x.Owner));25 26 Console.ReadKey();27 28 }29 }
The expression above indicates finding a vehicle with a mileage greater than 200 in a vehicle sequence. The next Code uses a Lambda expression to print the name of the selected owner.
- Delayed execution and stream processing
When the preceding query expression is created, it does not process data immediately or access the original car sequence. Instead, it generates the query representation in the memory, which is called delayed execution. As shown in the following figure, sequence operations are started only when result. ToList () is executed.
A select expression statement is called projection.
1 result.Where(e => e.Mileage > 200).Select(e => e);
The query expression is translated into the above Code by the compiler. It can be seen that they are composed of extension methods and Lambda expressions. Parameters (in most cases) are Delegate types at any time. The compiler uses Lambda expressions as real parameters and tries its best to find a method with a proper signature. Let's take a look at our expressions.
1 var result = from e in list2 where e.Mileage > 2003 select e;
From, where, in, select are the context Keywords of the query expression, e is the range variable, list is the data source sequence, and select e is the return projection. Let's look at an example of return projection.
1 var result1 = from e in list2 where e.Mileage > 2003 select e.Owner;
Select e. Owner here, that is, the name of each car Owner is returned. The type of result1 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()); //620 Console.WriteLine(oftypeList.Count()); //5
Cast <T> () converts the elements in the list to the T type. If an element cannot be converted, an error is returned. OfType <T> () it will try to convert each element to the T type, and skip if it encounters an element that cannot be converted. When the range variable is explicitly declared, the Cast () method is called during translation. For example
1 var result2 = from Car e in list2 select e;3 result2 = list.Cast<Car>().Select(e => e);
The expression is translated into the third line of code.
Please make an axe.