Preface
I recently conducted a technical interview with our boss (I attended this interview) and found that I was not familiar with the interview or even know about LINQ at ding. I am wondering if you have no time to understand or learn about the basics of this application since the release of LINQ with 2008. I even asked some people what the LINQ program is, and the respondents did not want to think about it, and they crashed! That's right. You can use the SQL statement to write data to the SQL statement? None. Therefore, I would like to share with you how to learn LINQ. This article is suitable for the following readers.
- I have never touched any of
- I have known about LINQ but never used it in practice.
- Intended to learn
Introduction
What is LINQ? Reference the official term"Language integration query (LINQ) is an innovative feature introduced in Visual Studio 2008 and. NET Framework 3.5. It builds a bridge between the object field and the data field."Then, let's take a look at the following examples of what LINQ has brought to us:
Q: sequence a = int [] {1, 2, 4, 5, 6, 7, 8, 0}; B = int [] {2, 4, 7, 8, 9 }. Requests a sequence C that contains the common values of A and B.
According to the original idea, the encoding may be as follows:
List<int> c = new List<int>();foreach(int a in A){ foreach(int b in b) { if (a==b) {
c.add(a);
} }}
Do you think the above section is okay, but it is ugly. What if we compile the code by referencing LINQ:
IEnumerable<int> C = from a in A from b in B where a==b select a;
Isn't it so nice? Simply put the ugly code above to Ko. You may not understand the above syntax. It doesn't matter. Please continue reading.
Grammar
1. The main namespace in which LINQ is located: system. LINQ
2. The core object processed by LINQ is the ienumerable enumerated object, which also includes generic enumeration. In other words, when the object to be processed is an ienumerable object, you can use LINQ to operate it. A new ienumerable sequence will be returned without any other processing. Note that the "delayed loading" feature of LINQ will be described later.
3. keywords (from msdn ):
From: Specifies the data source and range variables (similar to iteration variables ).
Where: filters source elements based on one or more boolean expressions separated by the logical "and" or "Operator (& |.
Select: specifies the type and format of the elements in the returned sequence when the query is executed.
GROUP: groups query results based on specified key values.
Into: provides an identifier that can be used as a reference to the results of join, group, or select clauses.
Orderby: sort the query results in ascending or descending order based on the default comparator of the element type.
Join: Join two data sources based on the equal comparison between two specified matching conditions.
Let: Introduce a range variable used to store the results of the subexpression in the query expression.
In: context keyword in the join clause.
On: context keyword in the join clause.
Equals: context keyword in the join clause.
By: context keyword in the group clause.
Ascending: context keyword in the orderby clause.
Descending: context keyword in the orderby clause.
4. syntax description, each LINQ statement is started with from, with select as the end, this point and the T-SQL syntax does not have to remember first thinking. Other keywords such as where are similar to T-SQL as filter criteria.
Example: ienumerable <t> Nums = from N in Nums where... orderby... select ....
Expansion
Starting from. Net 3.0, MS has introduced some other new features to us. Due to the space relationship, here we will give you a brief introduction to several frequently-used features of LINQ:
1. Keyword var:
Indicates the compiler to deduce the type of the variable based on the expression on the right of the initialization statement. The inference type can be a built-in type, an anonymous type, a user-defined type, or a type defined in the. NET Framework class library.In this way, we can use the preceding LINQ expression for example: var Nums = from N in Nums where... orderby... select ....
2. Anonymous type:
The anonymous type provides a convenient method to encapsulate a set of read-only attributes into a single object without explicitly defining a type.The type name is 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
Common Query
var query = from num in num select num.ProperyA
Filter Query
var query = from obj in objs where obj.ProperyA > Condition select obj
Group Query
var query = from obj in objs group obj by obj.PropertyA into g orderby g.Key select g;
Note: In this example, the keyword into is not mandatory. When using into, you must continue to write the query and end the query with one 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 outer 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: here the new. Net 3.5 static extension method is involved (will not affect understanding later) defaultifempty (): If the sequence is empty, a single set of instances with default values will be returned.
Summary
This article briefly introduces the basics of LINQ. You can perform simple practical applications. In the future, we will share with you how to learn static extension methods and how they relate to LINQ. Applications related to LINQ, such as: LINQ to SQL, lint to entites, LINQ to XML, and LINQ to dataset. Coming soon.
Finally, I would like to thank you for reading this article. please correct me for anything wrong.
Resource Source
Linqpad: a good tool for learning LINQ http://www.linqpad.net/
LINQ Development Resource: http://www.codeproject.com/KB/linq/