LINQ stands for Language Integrated Query. LINQ is a data querying API that provides querying capabilities to .NET languages with a syntax similar to a Squốc lộ. LINQ queries use C# collections to lớn return data.

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

LINQ in C# is used to lớn work with data access from sources such as objects, data sets, Squốc lộ Server, and XML. LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL lượt thích query syntaxes. LINQ provides functions to lớn query cached data from all kinds of data sources. The data source could be a collection of objects, database or XML files. We can easily retrieve sầu data from any object that implements the IEnumerable interface.
The official goal of the LINQ family of technologies is to add "general purpose query facilities to lớn the .NET Framework that apply khổng lồ all sources of information, not just relational or XML data".

LINQ offers an object-based, language-integrated way khổng lồ query over data no matter where that data came from. So through LINQ we can query database, XML as well as collections. Compile time syntax checking It allows you to lớn query collections like arrays, enumerable classes etc in the native language of your application, lượt thích VB or C# in much the same way as you would query a database using SQL
LINQ to lớn Object provides functionality to query in-memory objects và collections.Any class that implements the IEnumerable interface (in the System.Collections.Generic namespace) can be queried with SQO.

Xem thêm: Có Răng Mà Chẳng Có Mồm. Nhai Cỏ Nhồn Nhồn Cơm Chẳng Chịu Ăn. (Là Cái Gì?).

LINQ khổng lồ ADO.NET đơn hàng with data from external sources, basically anything ADO.NET can connect lớn. Any class that implements IEnumerable or IQueryable (in the System.Query namespace) can be queried with SQO.
LINQ lớn SQL (DLinq) Queries performed against the relation database only Microsoft Squốc lộ Server Supported LINQ lớn DataSet Supports queries by using ADO.NET data sets & data tables LINQ khổng lồ Entities
Here is a simple example that creates a array of integers. A LINQ query is used to return a var that stores the collection of returned data. Learn more:The var keyword in C#.
Let"s look at a working example. Create a Web page or UI with a GridView control that we will use khổng lồ display some data. The following code example defines a class, patient with some properties such as name, gender, age, and area.
publicclasspatient publicpatient() //Fields privatestring_name; privateint_age; privatestring_gender; privatestring_area; //Properties publicstringPatientName getreturn_name; set_name=value; publicstringArea getreturn_area; set_area=value; publicStringGender getreturn_gender; set_gender=value; publicintAge getreturn_age; set_age=value;
Now, on the Web Page (ASP..NET Web Forms in this case), we create a List object dynamically. This program adds a single record but you an add a collection of records. Once the collection is ready, LINQ can be used to lớn query the collection.
usingSystem.Collections.Generic; publicpartialclass_Default:System.Web.UI.Page protectedvoidPage_Load(objectsender,EventArgse) Listpat=newList(); patientp=newpatient(); p.patientname="Deepakdwij"; p.patientstate="UP"; p.patientage="25"; p.patientcity="Noida"; pat.Add(p); GridView1.DataSource=fromainpatselecta; GridView1.DataBind(); //GridView1.DataSource=frompainpatients //wherepa.Gender=="Male" //orderbypa.PatientName,pa.Gender,page authority.Age //selectpa; //GridView1.DataBind();
The following code uses the selection operator type, which brings all those records whose age is more than đôi mươi years.
varmypatient=frompainpatients wherepage authority.Age>20 orderbypa.PatientName,page authority.Gender,pa.Age selectpa; foreach(varppinmypatient) Debug.WriteLine(pp.PatientName+""+pp.Age+""+pp.Gender);
The following code snippet uses the grouping operator type that group patient data on the bases area.
varop=frompainpatients grouppabypa.Areaintog selectnewarea=g.Key,count=g.Count(),allpatient=g; foreach(varginop) Debug.WriteLine(g.count+","+g.area); foreach(varling.allpatient) Debug.WriteLine(" "+l.PatientName);
intpatientCount=(frompainpatients wherepage authority.Age>đôi mươi orderbypage authority.PatientName,pa.Gender,pa.Age selectpa).Count();
int<>numbers=5,4,1,3,9,8; varnumsPlusOne=fromninnumbersselectn; foreach(variinnumsPlusOne) MessageBox.Show(i.ToString());
int<>numbersA=0,2,4,5,6,8,9; int<>numbersB=1,3,5,7,8; varpairs=fromainnumbersAfrombinnumbersBwhereanewa,b; Console.WriteLine("Pairswherea); foreach(varpairinpairs) Console.WriteLine("0islessthan1",pair.a,pair.b);
string<>words="cherry","apple","blueberry"; varsortedWords=fromwinwordsorderbywselectw; Console.WriteLine("Thesortedlistofwords:"); foreach(varwinsortedWords) Console.WriteLine(w);
int<>factorsOf300=2,2,3,5,5; intuniqueFactors=factorsOf300.Distinct().Count(); Console.WriteLine("Thereare0uniquefactorsof300.",uniqueFactors);
int<> numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; int oddNumbers = numbers.Count(n => n % 2 == 1); Console.WriteLine("There are 0 odd numbers in the các mục.", oddNumbers);