LINQ introduces a standard, unified, easy-to-learn approach for querying and modifying data. In this article, you'll learn basics of LINQ and how khổng lồ use LINQ in C#.

Writing software means that you need khổng lồ have sầu a database sitting at the bachồng kết thúc, and most of the time goes inkhổng lồ writing queries to lớn retrieve & manipulate data. Whenever someone talks about data, we tover lớn only think of the information that is contained in a relational database or in an XML document.

Bạn đang xem: Linq c# là gì

The kind of data access that we had prior to the release of .NET 3.5 was only meant for or limited khổng lồ accessing data that resides in traditional data sources as the two just mentioned. But with the release of .NET 3.5 and higher versions lượt thích .NET 4.0 and .NET 4.5, that has Language INtegrated Query (LINQ) incorporated inlớn it, it is now possible to giảm giá khuyến mãi with data residing beyond the traditional homes of information storage. For instance, you can query a generic List type containing a few hundred integer values and write a LINQ expression to retrieve the subset that meets your criterion, for example, either even or odd.
The LINQ feature, as you may have sầu gathered, was one of the major differences between .NET 3.0 and .NET 3.5. LINQ is a mix of features in Visual Studio that extends powerful query capabilities into lớn the language syntax of C# & VB .NET
LINQ introduces a standard, unified, easy-to-learn approach for querying and modifying data, và can be extended to tư vấn potentially any type of data store. Visual Studio also supports LINQ provider assemblies that enable the use of LINQ queries with various types of data sources including relational data, XML, và in-memory data structures.
Introduction khổng lồ LINQArchitecture of LINQUsing LINQ to lớn ObjectsUsing LINQ khổng lồ SQLUsing LINQ to XML

Introduction to lớn LINQ

LINQ is an innovation that Microsoft made with the release of Visual Studio 2008 & .NET Framework version 3.5 that promises to revolutionize the way that developers have been working with data before the release of .NET 3.5. Microsoft continued with the LINQ feature with the recent releases of .NET 4.0/4.5 & Visual Studio 2012. As I mentioned previously, LINQ introduces the standard và unified concept of querying various types of data sources falling in the range of relational databases, XML documents, & even in-memory data structures. LINQ supports all these types of data stores using LINQ query expressions of first-class language constructs in C#. LINQ offers the following advantages:
LINQ offers common syntax for querying any type of data source; for example, you can query an XML document in the same way as you query a SQL database, an ADO.NET dataset, an in-memory collection, or any other remote or local data source that you have sầu chosen to connect to lớn và access by using LINQ. LINQ bridges the gap & strengthens the connection between relational data and the object-oriented world. LINQ speeds development time by catching many errors at compile time và including IntelliSense và debugging tư vấn. LINQ query expressions (unlike traditional Squốc lộ statements) are strongly typed.
The LINQ assemblies provide all the functionality of accessing various types of data stores under one umbrella. The core LINQ assemblies are listed in Table đơn.
Assembly Name Description
System.LINQ Provides classes & interfaces that tư vấn LINQ queries
System.Collections.Generic Allows users khổng lồ create strongly typed collections that provide better type safety và performance than nongeneric strongly typed collections (LINQ to Objects)
System.Data.LINQ Provides the functionality lớn use LINQ lớn access relational databases (LINQ to lớn SQL)
System.XML.LINQ Provides functionality for accessing XML documents using LINQ (LINQ to lớn XML)
System.Data.Linq.Mapping Designates a class as an entity class associated with a database

LINQ to Objects LINQ to ADO.NET, that includes LINQ lớn SQL (formerly called DLinq) LINQ khổng lồ DataSets (formerly called LINQ over DataSets) LINQ to Entities LINQ lớn XML (formerly called XLinq)
Figure đối kháng depicts the LINQ architecture, that clearly shows the various components of LINQ & their related data stores.

LINQ lớn Objects giao dịch with in-memory data. Any class that implements the IEnumerable interface (in the System.Collections.Generic namespace) can be queried with Standard Query Operators (SQOs).

Xem thêm: Cách Hack Game Android Online

LINQ to lớn ADO.NET (also known as LINQ-enabled ADO .NET) giao dịch with data from external sources, basically anything ADO.NET can connect to. Any class that implements IEnumerable or IOueryable (in the System.Linq namespace) can be queried with SQOs. The LINQ to lớn ADO.NET functionality can be done by using the System. Data.Linq namespace.
LINQ to lớn XML is a comprehensive sầu API for in-memory XML programming. Like the rest of LINQ, it includes SQOs, & it can also be used in concert with LINQ lớn ADO.NET, but its primary purpose is to lớn unify & simplify the kinds of things that disparate XML tools, such as XQuery XPath, & XSLT, are typically used khổng lồ bởi. The LINQ to lớn XML functionality can be done by using the System.Xml.Linq namespace.
In this article, we"ll work with the three techniques LINQ lớn Objects, LINQ khổng lồ SQL, & LINQ khổng lồ DataSets.
The term LINQ lớn Objects refers khổng lồ the use of LINQ queries khổng lồ access in-memory data structures. You can query any type that supports IEnumerable. This means that you can use LINQ queries not only with user-defined lists, arrays, dictionaries, and so on, but also in conjunction with .NET Framework APIs that return collections. For example, you can use the System.Reflection classes lớn return information about types stored in a specified assembly, và then filter those results using LINQ. Or you can import text files inlớn enumerable data structures & compare the contents to lớn other files, extract lines or parts of lines, group matching lines from several files into lớn a new collection, và so on. LINQ queries offer three main advantages over traditional foreach loops:
They are more concise & readable, especially when filtering multiple conditions. They provide powerful filtering, ordering, and grouping capabilities with a minimum of application code. They can be ported khổng lồ other data sources with little or no modification.
In general, the more complex the operation you want to lớn persize on the data, the greater the benefit you will realize using LINQ as opposed khổng lồ traditional iteration techniques.
In this exercise, you"ll create a Windows Forms Application having one Text Box. The application will retrieve sầu và display some names from an array of strings in a TextBox control using LINQ khổng lồ Objects.
Open Visual Studio 2012 and select "File" -> "New" -> "Project...". Choose "Windows Forms" project. Right-cliông xã the "Form1.cs" in the solution, select "Rename" and rename the size to "LinqToObjects". Drag a Text Box control onkhổng lồ the form, and position it towards the center of the Form. Select this Text Box và navigate khổng lồ the Properties window. Now your LinqToObjects khung in Design view should be such as shown in Figure 1-2.
Figure 1-2. Design view of LinqToObjects form Now double-click on the empty surface of the "LinqToObjects.cs" form & it will open the code editior window, showing the "LinqToObject_Load" sự kiện. Place the following code in the "LinqToObjects_Load" sự kiện as shown in Listing đối kháng.
//Definestringarray string<>names="LifeisBeautiful", "ArshikaAgarwal", "SevenPounds", "RupaliAgarwal", "PearlSolutions", "VamikaAgarwal", "VidyaVratAgarwal", "naijanewsmag.comMumbaiChapter" ; //Linqquery IEnumerablestring>namesOfPeople=fromnameinnames wherename.Length selectname; foreach(varnameinnamesOfPeople) txtDisplay.AppendText(name+" ");

Navigate khổng lồ Solution Explorer, right-cliông xã your LINQ project and select "Add Windows Form". In the "Add New Item" dialog make sure "Windows Form" is selected và then rename the "Form1.cs" lớn "LinqToSql". Cliông xã "Add". Drag a Text Box control onto lớn the size, and position it at towards the center of the Form. Select this Text Box và navigate khổng lồ the Properties window, và phối the following properties: Name khổng lồ txtLinqToSql. Multiline lớn True. ScrollBars lớn Vertical. Now your LinqToSql form in Design view should be such as shown in Figure 1-4.
Figure 1-4. Design view of LinqToSql khung Before we begin with coding the functionality, we must add the required assembly references. LinqToSql will require an assemble reference of System.Data.Linq lớn be added to lớn the LINQ project. To vì so, in Solution Explorer, select the "References", right-click and choose "Add Reference". From the opened Reference Manager dialog, scroll down khổng lồ the assembly danh sách select System.Data.Linq and kiểm tra the checkbox shown in front of it as shown in figure 1-5 and cliông chồng "OK".
Figure 1-5. Adding LINQ References xuất hiện the newly added form "LinqToSql.cs" in code view. Add the code shown in Listing 1-2 lớn LinqToSql.cs. Listing 1-2. LinqToSql.cs
Now, lớn mix the LinqToSql size as the startup form, open Program.cs in the code editor and modify the following: Application.Run(new LinqToObjects()); to appear as: Application.Run(new LinqToSql());. Build the solution và then run the program by pressing Ctrl+F5, và you should see the results shown in Figure 1-6.
Figure 1-6. Retrieving liên hệ details with LINQ khổng lồ SQL
Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *