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 SQL. LINQ queries use C# collections to return data.
Đang xem: Linq c# là gì
LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL like query syntaxes. LINQ provides functions to 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 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 the .NET Framework that apply to all sources of information, not just relational or XML data”.

LINQ offers an object-based, language-integrated way to 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 query collections like arrays, enumerable classes etc in the native language of your application, like VB or C# in much the same way as you would query a database using SQL
LINQ to Object provides functionality to query in-memory objects and collections.Any class that implements the IEnumerable interface (in the System.Collections.Generic namespace) can be queried with SQO.
Xem thêm: “Multiple Coefficient Of Determination Là Gì ? Coefficient Of Determination Là Gì
LINQ to ADO.NET deals with data from external sources, basically anything ADO.NET can connect to. Any class that implements IEnumerable or IQueryable (in the System.Query namespace) can be queried with SQO.
LINQ to SQL (DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported} LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables} LINQ to 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 to 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 { get{return_name;} set{_name=value;} } publicstringArea { get{return_area;} set{_area=value;} } publicStringGender { get{return_gender;} set{_gender=value;} } publicintAge { get{return_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 query the collection.
Xem thêm: Lợi Ích Của Referral Là Gì ? Referral Đem Lại Lợi Ích Gì Cho Bạn?
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,pa.Age //selectpa; //GridView1.DataBind(); } }
The following code uses the selection operator type, which brings all those records whose age is more than 20 years.
varmypatient=frompainpatients wherepa.Age>20 orderbypa.PatientName,pa.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 selectnew{area=g.Key,count=g.Count(),allpatient=g}; foreach(varginop) { Debug.WriteLine(g.count+”,”+g.area); foreach(varling.allpatient) { Debug.WriteLine(” “+l.PatientName); } }
intpatientCount=(frompainpatients wherepa.Age>20 orderbypa.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=fromainnumbersAfrombinnumbersBwhereanew{a,b}; Console.WriteLine(“Pairswherea); foreach(varpairinpairs) { Console.WriteLine(“{0}islessthan{1}”,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(“Thereare{0}uniquefactorsof300.”,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 list.”, oddNumbers);