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

What is the format of the query statement in LINQ

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

Share

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

This article is to share with you about the format of query statements in LINQ. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The basic format of LINQ is as follows:

Var = from in where orderby

LINQ basic clause

From query clause-- Foundation

Followed by the project name and data source

The sample code is as follows:

Var str = from lq in str select lq

Where the select statement specifies which data source the element returned to the collection variable comes from

From query clause-nested query

You can nest another from clause within the from clause, as shown in the sample code below.

Var str = from lq in str from m in str2 select lq

Where conditional clause

The where clause specifies the filter condition, which means that the code snippet in the where clause must return a Boolean value to filter the data source

The sample code is as follows:

Var str = from m in MyList where m.Length > 5 select m

Where clause query

When multiple where clauses are needed for compound conditional queries, "& &" can be used to integrate where clauses.

The sample code is as follows:

Var str = from m in myList where (m.Length > 6 & & m.Contains ("liu")) select m

Group grouping clause

Statement format: var str = from p in PersonList group p by p.age

The group clause groups the data in the data source, and when traversing the data element, it does not traverse the element directly as in the previous chapter, because the group clause returns a sequence of objects of element type IGrouping, and an object loop must be nested in the loop to query the corresponding data element.

When using the group clause, there is no select clause at the end of the LINQ query clause, because the group clause returns an object sequence, and the corresponding object elements can be found in the object sequence through loop traversing. If you use the group clause for grouping operation, you can not use the select clause.

Orderby sort clause

Statement format: var str = from p in PersonList orderby p.age select p

Use the descending keyword in reverse order in the orderby clause

The sample code is as follows:

Var str = from p in PersonList orderby p.age descending select p

The orderby clause can also sort multiple conditions by dividing them with the sign ",".

The sample code is as follows:

Var str = from p in PersonList orderby p.age descending,p.name select p

Join join clause

In LINQ, you can also use the join clause to query related data sources or data objects, but first the two data sources must be related to each other.

Var str = from p in PersonList join car in CarList on p.cid equals car.cid select p

Thank you for reading! This is the end of this article on "what is the format of query sentences in LINQ?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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