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

How to use LINQ query expressions

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the LINQ query expression, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The from clause is responsible for specifying the data source and scope variables in the LINQ query operation

1. Every LINQ query expression must contain a from clause and must begin with a from clause.

two。 If the LINQ query expression also contains a subquery, then the subquery expression must also start with the from clause.

3. The data source includes not only the data source of the LINQ query itself, but also the data source of the subquery. Range variables are generally used to represent each element in the source sequence.

The type of the data source specified by the 4.from clause must be IEnumerable, IEnumerable, or a derived type of the first two.

5. In the from clause, if the data source implements IEnumerable, the compiler can automatically infer the type of range variable. However, if the type of the data source is a non-generic IEnumerable type (such as ArrayList, etc.), you must explicitly specify the data type of the range variable.

An example of compound from clause query

In some cases, each element of the data source itself may also contain another child data source (such as sequences, lists, and so on). At this point, if you want to query elements in a child data source, you need to use a compound type from clause.

The following example demonstrates the method of compound from clause query, the specific steps are as follows.

1. Create a data source with the data type List. Where the data type of the Scores attribute of the students element is List, that is, the value of the attribute is also a child data source.

two。 Use the compound from clause to query each student's score information for each subject greater than 90 points. The first from clause is used to query the students data source, and the second from clause is used to query the student.Scores data source.

3. Use the foreach statement to output the results of the query and finally display the results in the ASP.NET server label control.

Public class Student {public string LastName {get; set;} public List Scores {get; set;}. (omitted) StringBuilder str = new StringBuilder ("") / establish data source List students = new List {new Student {LastName= "Omelchenko", Scores= new List {97,97,81,60}}, new Student {LastName= "O'Donnell", Scores= new List {75,80,91,39}}, new Student {LastName= "Mortensen", Scores= new List {88,94,65,85}}, new Student {LastName= "Garcia", Scores= new List {99,99,82}}, new Student {LastName= "Beebe", Scores= new List {35,94,91) 70} / / use the compound from clause to cycle through each student's scores greater than 90 points var scoreQuery = from student in students from score in student.Scores where score > 90 select new {Last = student.LastName, score}; / / display the query results foreach (var v in scoreQuery) {str.Append (v.Last + "" + v.score+ ");} Label1.Text ="; Label1.Text = str.ToString () Thank you for reading this article carefully. I hope the article "how to use LINQ query expressions" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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