[C #] foundation Linq operator instructions:

1: Screening

Where: where clause, you can combine multiple expressions. For example: find winning at least 15 games in the United States and the British Racing Drivers

   var racers = from r in Formulal.GetChampions() 
       where r.Wins > 15 && (r.Country == "USA" || r.Country == "UK")
          select r; foreach (var r in racers) { Console.WriteLine($"{r:A}"); }

 

 The same result:

 var racers2 = Formulal.GetChampions().Where(r => r.Wins > 15 && (r.Country == "USA" || r.Country == "UK")).Select(r=>r);

  

Wherein r: A is designated A

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/formatting-numeric-results-table

You can be created using the format specifier format string. The format string is as follows: Axx, wherein

  • A Is the format specifier, the control value applied to the type of formatting.
  • xx Is the precision specifier Effect of bits formatted output. Description Range Accuracy symbol values is 0 to 99.

 2: Index Filter

In where () method overloading can pass the second parameter - the index.

Counters for each result returned when the index filter. You can use this index in the expression. Perform calculations based on the index.

1    // Query racers all surnames beginning with A, and the index is odd racer 
2              var racers3 = Formulal.GetChampions () 
        .Where ((R & lt, index) => r.LastName.StartsWith ( " A " ) && % index 2 ! = 0 ); . 3 the foreach ( var Racer in racers3) . 4 { . 5 Console.WriteLine ($ " {Racer: A} " ); . 6 }

3: Type Screening

Based on the type of filter to be used OfType () extension method.

 1             object[] data = {"one", 1, 3, "four", "five", 6};
 2             var query = data.OfType<string>();
 3             Console.WriteLine("输出string类型");
 4             foreach (var s in query)
 5             {
 6                 Console.WriteLine(s);
 7             }
 8 
 9             Console.WriteLine("输出int类型");
10             var query2 = data.OfType<int>();
11             foreach (var q in query2)
12             {
13                 Console.WriteLine(q);
14             }

 

 

4: Compound from clause 

If you need to filter by a member of the object, which is itself a member of a series, you can use a composite from clause

Cars in the following examples is an attribute of r, is an array of strings Cars

// If desired screening in accordance with one object member, which itself is a member of a family, can use the composite from clause 
            var ferrariDrivers = from R & lt in Formulal.GetChampions ()
                 from C in r.Cars
                 WHERE C == " ferrari " 
                OrderBy r.LastName
                 SELECT r.FirstName + "  " + r.LastName;
             the foreach ( var Item in ferrariDrivers) 
            { 
                Console.WriteLine (Item); 
            }    

 

c # compiler and the compound from clause queries into Linq SelectMany () extension method.

SelectMany () method can be used to sequence iterative sequence.

1  Console.WriteLine("=====SelectMany查询方法======");
2             var ferrariDrivers1 = Formulal.GetChampions().SelectMany(r => r.Cars, (r, c) => new {Racer = r, Car = c}).Where(r=>r.Car=="Ferrari").OrderBy(r=>r.Racer.LastName).Select(r=>r.Racer.FirstName+" "+r.Racer.LastName+" " +r.Car);
3             foreach (var item in ferrariDrivers1)
4             {
5                 Console.WriteLine(item);
6             }

The methods and results from the composite return is the same clause.

 

 5: Sorting

Orderby clause and the clause orderby descending

 1 var racers = from r in Formulal.GetChampions()
 2                 where r.Country == "Italy"
 3                 orderby r.Wins descending
 4                 select r;
 5             foreach (var racer in racers)
 6             {
 7                 Console.WriteLine(racer);
 8             }
 9 
10             Console.WriteLine();
11             var racers1 = Formulal.GetChampions().Where(r => r.Country == "Italy").OrderByDescending(r => r.Wins);
12             foreach (var racer in racers1)
13             {
14                 Console.WriteLine(racer);
15             }

 Description: OrderBy () and OrderByDescending () method returns IOrderEnumerable <TSource> is derived from this interface.

IEnumerable <TSource> interface. But includes an additional method CreateOrderedEnumerable <TSource> (). This method is used to further ordered sequence. If the sorting keyword selector, two of which have the same, you can use the ThenBy () ThenByDescending, and () method continues sorting. Can add a plurality of the ThenBy () and ThenByDesceding () method to order the collection.

 1  var racers2 =
 2                 (from r in Formulal.GetChampions() orderby r.Country, r.LastName, r.FirstName select r).Take(10);
 3             foreach (var racer in racers2)
 4             {
 5                 Console.WriteLine(racer);
 6             }
 7 
 8             Console.WriteLine();
 9 
10             var racer3 = Formulal.GetChampions().OrderBy(r => r.Country).ThenBy(r => r.LastName)
11                 .ThenBy(r => r.FirstName).Take(10);
12             foreach (var racer in racer3)
13             {
14                 Console.WriteLine(racer);
15             }

 6: Grouping

According to a key value for grouping query results, you can use the group clause

Guess you like

Origin www.cnblogs.com/SignX/p/11448977.html
Recommended