Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An overview of the basic syntax of Linq

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report