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 implement compound from clause in LINQ query expression

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

Share

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

This article mainly shows you the "LINQ query expression in how to achieve the compound from clause", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "LINQ query expression in how to achieve the compound from clause" this article.

1. Function

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

two。 Grammatical requirements

① every LINQ query expression must contain a from clause and must begin with a from clause.

② if the LINQ query expression also contains a subquery, then the subquery expression must also begin with the from clause.

③ data sources include not only the data sources of the LINQ query itself, but also the data sources of subqueries. Range variables are generally used to represent each element in the source sequence.

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

⑤ 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.

3.

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.

(2) use the compound from clause to query the subject score information of each student which is 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 result of the query, and finally display the result 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 to search out 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 ()

The following figure shows a snapshot of the running results of the LINQ query in the above example.

These are all the contents of the article "how to implement compound from clauses in LINQ query expressions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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