The purpose of this article is to share with netizens the practices of Linq in projects. Projects I have participated in can see the shadows of Linq. (LinqTosql, LinqToString, LinqToXML, LinqToEntity, and so on...) I personally think that Linq is a very successful technology launched by Microsoft in recent years. For example, when I am familiar with Linq and use SQL to write business logic, I feel that I am no longer used to it. If you have not studied Linq in the system, I hope this article will help you.
This article will introduce the application scope of Linq in actual development from the following aspects:
1. LinqToXML
Add an xml file to the test project:
You can add, delete, modify, and query XML files by operating xml files with the following code:
2. LinqToEntity
Use EF to operate the NorthWind database:
Note: The generated SQL statements are:
Inner Join:SELECT \ r \ n1 AS [C1], \ r \ n [Extent1]. [CompanyName] AS [CompanyName], \ r \ n [Extent2]. [OrderDate] AS [OrderDate] \ r \ nFROM [dbo]. [MERs] AS [Extent1] \ r \ nINNER JOIN [dbo]. [Orders] AS [Extent2] ON [Extent1]. [CustomerID] = [Extent2]. [CustomerID]
Outer Join:SELECT \ r \ n1 AS [C1], \ r \ n [Extent1]. [CompanyName] AS [CompanyName], \ r \ n [Extent2]. [OrderDate] AS [OrderDate] \ r \ nFROM [dbo]. [MERs] AS [Extent1] \ r \ nLEFT outer join [dbo]. [Orders] AS [Extent2] ON [Extent1]. [CustomerID] = [Extent2]. [CustomerID]
3. LinqToFile
To operate disk files using Linq:
4. LinqToArray
Array of Linq operations: