Get the App
SLTechnology News&Howtos  ›  Development  › 

An overview of the basic syntax of Linq

Shulou Source: shulou.com Published: 2022-06-03 20:03:51 10月04日 Update

This article mainly introduces "basic grammar overview of Linq". In daily operation, I believe many people have doubts about the basic grammar overview of Linq. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "basic grammar overview of Linq"! Next, please follow the editor to study!

Before I give you a detailed introduction to the basic syntax of Linq, I will first let you know about calling the Enumberalbe extension function, and then give a comprehensive introduction to the basic syntax of Linq.

Basic syntax of Linq

Var result = from item in container orderby value ascending/descending select item

1. Get all the records

Var allCars = from c in myCars select c

2. Get only the field name

Var names = from c in myCars select c.PetName

Here names is an implicitly typed variable.

3. Use Enumerable.Distinct ()

Var makes = from c in myCars select c.Make) .Distinct ()

4. You can call the Enumberalbe extension function when you define it.

Var names = from c in myCars select c.PetName; foreach (var n in names) {Console.WriteLine ("Name: {0}", n);}

Can also be called on compatible array types

Var makes = from c in myCars select c.Make; Console.WriteLine ("Distinct makes:"); foreach (var m in makes.Distinct ()) {Console.WriteLine ("Make: {0}", m);} / / Now get only the BMWs. Var onlyBMWs = from c in myCars where c.Make = = "BMW" select cinterbank / Get BMWs going at least 100 mph. Var onlyFastBMWs = from c in myCars where c.Make = = "BMW" & & c.Speed > = 100select c

5. Generate new data types (projection)

Var makesColors = from c in myCars select new {c.Make, c.Color}

6. Reverse ()

Or

Var subset = (from c in myCars select c) .Reverse (); foreach (Car c in subset) {Console.WriteLine ("{0} is going {1} MPH", c.PetName, c.Speed);}

7. Sort

Default is ascending

/ / Order all the cars by PetName. Var subset = from c in myCars orderby c.PetName select c; / / Now find the cars that are going less than 55 mph, / / and orderby descending PetName subset = from c in myCars where c.Speed > 55 orderby c.PetName descending select c

The default order can also be explicitly specified

Var subset = from c in myCars orderby c.PetName ascending select c

8. Enumerable.Except ()

The difference between two IEnumerable-compatible objects

Static void GetDiff () {List myCars = new List {"Yugo", "Aztec", "BMW"}; List yourCars = new List {"BMW", "Saab", "Aztec"}; var carDiff = (from c in myCars select c) .Except (from c2 in yourCars select c2); Console.WriteLine ("Here is what you don't have, but I do:"); foreach (string s in carDiff) Console.WriteLine (s); / / Prints Yugo. At this point, the study of "an overview of the basic grammar of Linq" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Syntax learning types functions more help practical next two variables names fields objects that is subtractions data arrays articles methods time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology MySQL NVidia Shulou Information