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

Basic Analysis of LINQ query Syntax

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "basic analysis of LINQ query syntax". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Set initializer

Public class Person {public string username {get; set;} public int age {get; set;} public override stringToString () {return string.Format ("username: {0} age: {1}", this.username, this.age);} var persons = new List {new Person {username = "a", age=1}, new Person {username = "b", age=2}}; foreach (var p in persons) Console.WriteLine (p.ToString ())

The compiler will automatically insert the collection for you. If you initialize for Hashtable, you are using two object initializers.

LINQ query syntax

Var persons = new List {new Person {username = "a", age=19}, new Person {username = "b", age=20}, new Person {username = "a", age=21},}; var selectperson = from p in persons where p.age > = 20 select p.username.ToUpper (); foreach (var p in selectperson) Console.WriteLine (p)

LINQ query syntax is a convenient declarative simplification when using standard LINQ query operators to express queries. This syntax can improve readability and simplicity when expressing queries in code, and it is easy to read and write correctly. Visual Studio provides complete intelligent sensing and compile-time checking support for query syntax. At the bottom, the compiler translates the expression of the query syntax into explicit method call code, which is implemented by new extension methods and Lambda expression language features. The above query syntax is equivalent to the following code:

Var selectperson = persons.Where (p = > p.age > = 20) .Select (p = > p.username.ToUpper ())

LINQ query syntax can achieve more than 90% of the function of T-SQL (because T-SQL is based on two-dimensional tables, the query syntax of LINQ is simpler and more flexible than T-SQL), but due to intelligent sensing, select cannot be entered at the beginning.

This is the end of the introduction to the basic analysis of LINQ query syntax. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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