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 are the ways in which LINQ expressions specify the target data

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

Share

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

This article mainly explains the "LINQ expression to specify the target data what are the methods", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "LINQ expression to specify the target data method" bar!

LINQ expression is an important part of LINQ function, which is generally realized through related keywords to query, modify, delete and other functions. These keywords are generally similar to those controlled by the database, such as from, select, where and so on.

1.LINQ expression keyword

Keyword function

From specifies the data source and scope variables to find, and multiple from clauses indicate finding data from multiple data sources

Select specifies the target data to be returned by the query, and can specify any type, even anonymous type

Where specifies the filter criteria for elements, while multiple where clauses represent juxtaposition conditions, all of which must be met in order to be selected.

Orderby specifies the sorting field and how the elements are sorted. When there are multiple sort fields, the primary and secondary relationship is determined by the field order, and two sorting methods, ascending and descending, can be specified.

Group specifies the grouping field of the element

Join specifies how multiple data sources are associated

The 2.LINQ expression specifies the data source with the from clause

Each LINQ query starts with a from clause, and the from clause includes the following two functions.

Specifies that the query will take the data source.

Define a local variable that represents a single element in the data source.

The format of a single from clause is as follows. Where dataSource represents the data source and localVar represents a single element.

From localVar in dataSource

The 3.LINQ expression uses the select clause to specify the target data

The select clause specifies the type of element in the dataset that produces the result when the query is executed, in the following format:

Select element

The 4.LINQ expression specifies the filter condition with the where clause

In LINQ, you use the where clause to specify the filter criteria for the query, which is in the following format:

Where expression where express is a logical expression that returns a Boolean value of "true" or "false". In a LINQ query, you can also use multiple juxtaposed where clauses for multiple conditional filtering. Elements in the data source can be used as query results only if they meet the conditions of all where clauses at the same time.

5.LINQ expressions are sorted using the orderby clause

In LINQ, the query results are sorted through the orderby clause, which has the following format:

Orderby element [sortType] where element is the field to be sorted, which can be an element in the data source or the result of an operation on an element. SortType is an optional parameter that indicates the sort type, including two optional values, ascending (ascending) and descending (desending). By default, it is ascending.

Note that the LINQ expression:

The orderby clause is different from the where clause. When there are multiple orderby clauses in a LINQ query, only one orderby clause is valid, and none of the previous orderby clauses is valid.

6.LINQ expressions are grouped with group clauses

In LINQ, the group clause is used to group the query results. In LINQ, the common format of the group clause is as follows:

Group element by key where element represents the element returned as the query result, and key represents the grouping condition. The group clause returns the query result of type IGrouping. Where the type of TKey is the data type of parameter key, and the type of TElement is the data type of parameter element.

Sometimes you need to sort the results of the grouping, query again, and so on. This requires the use of the into keyword to save the results of the group query to a temporary variable, and it must be re-queried with a new select or group clause, or you can use orderby for sorting, filtering with where, and so on. The syntax format of the into keyword is as follows:

Group element by key into tmpGrp where tmpGrp represents a local variable, which temporarily saves the results produced by group and provides the following LINQ expression clause to use.

Thank you for your reading, the above is the content of "what is the method of specifying target data by LINQ expression". After the study of this article, I believe you have a deeper understanding of what the method of specifying target data by LINQ expression has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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