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 entry-level knowledge points of LINQ

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

Share

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

This article mainly explains "what are the basic knowledge points of LINQ". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the basic knowledge points of LINQ"?

Brief introduction

What is LINQ? To quote the official term "language Integrated query (LINQ) is an innovative feature introduced in Visual Studio 2008 and .NET work 3.5, it bridges the gap between the object domain and the data domain." So what does LINQ bring to us? take a look at the following example:

Q: there is a sequence of A=int [] {1pr 2, 3je, 4je, 5g, 6, 7, 8, 0}; B=int [] {2, 4, 7, 8, 9}. The sequence C containing the common values of An and B is requested.

If you follow the original idea, the code might be as follows:

Description:

List c = new List (); foreach (int an in A) {foreach (int b in b) {if (a);}

Description:

Do you think the above paragraph is not a problem, but it is ugly? What if we quote LINQ to write:

IEnumerable C = from an in A from b in B where axioms select a

Grammar

1. The main namespace in which LINQ is located: System.Linq

2. The core object of LINQ's processing is IEnumerable enumerable objects, which also includes generic enumerations, in other words, when you want to deal with objects of type IEnumerable, you can use LINQ to operate it. And a new IEnumerable sequence will be returned without other processing. Note that LINQ has a feature called "delayed loading", which will be explained later.

3. Keywords (extracted from MSDN):

From: specify data source and scope variables (similar to iteration variables).

Where: filters source elements based on one or more Boolean expressions separated by logical and and or operators (& & or | |).

Select: specifies the type and form of elements in the sequence returned when the query is executed.

Group: groups query results according to the specified key values.

Into: provides an identifier that can serve as a reference to the result of a join, group, or select clause.

Orderby: the default comparator based on element type sorts query results in ascending or descending order.

Join: joins two data sources based on an equality comparison between two specified matching criteria.

Let: introduces a range variable that stores the results of a subexpression in a query expression.

In: the context keyword in the join clause.

On: the context keyword in the join clause.

Equals: the context keyword in the join clause.

By: the context keyword in the group clause.

Context keyword in the ascending:orderby clause.

Context keyword in the descending:orderby clause.

4. The syntax states that each LINQ statement begins with from and ends with select, which is not the same as the preconceived thinking of T-SQL syntax. Other keywords, such as where, are similar to T-SQL as a filter condition.

Example: IEnumerable nums = from n in nums where.... Orderby... Select....

Expand

MS has introduced some other new features to us since .net 3.0. Due to the lack of space, I will briefly introduce a few features commonly used in LINQ:

1. Keyword var:

Instructs the compiler to infer the type of variable from the expression to the right of the initialization statement. An inferred type can be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library. So we can abbreviate, for example, var nums = from n in nums where in the above LINQ expression. Orderby... Select....

two。 Anonymous type:

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without first explicitly defining a type. Type names are generated by the compiler and cannot be used at the source code level. The type of each attribute is inferred by the compiler. For example: var obj = new {A = "a", B = "b"}, while LINQ can be var nums = from obj in objs select new {obj.A, obj.B}

Case

General query

Var query = from num in num select num.ProperyA

Filter query

Var query = from obj in objs where obj.ProperyA > Condition select obj

Grouping query

Var query = from obj in objs group obj by obj.PropertyA into g orderby g.Key select g

Note that in this example, the keyword into is not required, and when using into, you must continue to write the query and end it with a select statement or another group clause.

Inline query

Var query= from obj1 in objs1 join obj2 in objs2 on obj1.ID equals obj2.ID select new {A = obj1.Property, B = obj2.Property}

Left Outreach query

Var query = from obj1 in objs1 join obj2 in objs2 on obj1.ID equals obj2.Obj1ID into g from subpet in g.DefaultIfEmpty () select new {P1 = obj1.P1, P2 = (subpet = = null? Null: subpet.P2)}

Note that this refers to the static extension method for the new features of .net 3.5 (the following introduction will not affect understanding) DefaultIfEmpty (): if the sequence is empty, a single instance collection with default values is returned

At this point, I believe you have a deeper understanding of "what are the basic knowledge points of LINQ?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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