Linq, it seems, is. NET original, Java, or any other development language, doesn’t seem to have this thing. The fate of Microsoft’s original creation is uncertain, and may soon be abandoned by Microsoft itself.

But for now, the young. NET programmers write code with Linq, Lambda expressions everywhere. Looking at the code, I felt unfamiliar and awed. Was I really getting old?

The reason is that I am not familiar with Linq.

But in fact, after reading an article on the Internet, I feel better understanding:

www.cnblogs.com/liulun/arch… \

\

The biggest takeaway from reading this article is that Linq has a lot to do with the interface IEnumerable:

Only a collection that implements IEnumerable can perform LINQ operations such as SELECT, WHERE, etc

That is, linQ operations support traversal. A LINQ operation is, essentially, a traversal operation.

It used to be that for a collection or an array, to find elements in it, we would loop, for, while, or foreach. So now that WE have LINq, we can write it a little bit more succinctly. That’s all. However, today’s programmers, because the division of labor is more and more fine, do not understand the database operation, SQL estimates too lazy to write, all ORM, LINQ up.

To be honest, LINQ is really similar to SQL. The most significant difference is that the SELECT part has been moved from the beginning to the end.

from v in arr where v > 3 select v\

The dotted lines below are taken from the reference article above:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Let’s start with some pseudocode:

[csharp]  view plain copy

  1. from [type] id in source  
  2. [join [type] id in source on expr equals expr [into subGroup]]  
  3. [from [type] id in source | let id = expr | where condition]  
  4. [orderby ordering,ordering,ordering…]  
  5. select expr | group expr by key  
  6. [into id query]  

Type is optional, id is an item in the collection, and source is a collection. If the type in the collection is different from the type specified by type, the cast will be forced. A query expression can have zero or more join clauses, where source expr is not equal to source expr is an expression [into subGroup] subGroup is an intermediate variable, which is derived from IGrouping and represents a group. That is to say, “many” \ in “one to many”

You can use this variable to get the number of objects in the group and the key of the group of objects

For example: \

[csharp]  view plain copy

  1. from c in db.Customers  
  2. join o in db.Orders on c.CustomerID
  3. equals o.CustomerID into orders
  4. select new
  5. {
  6. c.ContactName,
  7. OrderCount = orders.Count()
  8. };

<3> A query expression can have one or more FROM clauses. A query expression can have zero or more let clauses. The let clause creates a temporary variable \

Such as:

[csharp]  view plain copy

  1. from u in users  
  2. let number = Int32.Parse(u.Username.Substring(u.Username.Length – 1))
  3. where u.ID < 9 && number % 2 == 0
  4. select u

A query expression can have zero or more WHERE clauses. The WHERE clause can specify the query condition <4> A query expression must end with either SELECT followed by the content to be retrieved or group by, which groups the retrieved content such as: \

[csharp]  view plain copy

  1. from p in db.Products    
  2. group p by p.CategoryID into g    
  3. select new {  g.Key, NumProducts = g.Count()};   

<6> Explanation in line 6: The last into clause serves to use the results of previous statements as the data source for subsequent statements such as: \

[csharp]  view plain copy

  1. from p in db.Employees  
  2. select new
  3. {
  4. LastName = p.LastName,
  5. TitleOfCourtesy = p.TitleOfCourtesy
  6. } into EmployeesList
  7. orderby EmployeesList.TitleOfCourtesy ascending
  8. select EmployeesList;

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \

Take a look at some code:

Example 1:

[csharp]  view plain copy

  1. Var query = (from a in _dbcontext.set

    ()// _dbcontext.set

    () is a List object

  2.                     where a.ProjectID == projectId && a.Creator == account  
  3.                     select (new ViewTag  
  4.                     {  
  5.                         ViewId = a.ID  
  6.                         ,Name = a.Name  
  7.                         ,IsValid = a.IsValid  
  8.                         ,Seq = a.Seq  
  9.                         ,ChangeType = (byte)ViewTag.EChangeType.NoChanged  
  10.                     }));  
  11.             return query.ToList<ViewTag>();  

Example 2

[csharp]  view plain copy

  1. IEnumerable<Meta_View> dbViews = _DbContext.Set<Meta_View>()  
  2.     .Where(m => m.ProjectID.Equals(projectId) && m.Creator.Equals(account));  

\

\