Linq c# là gì

     

LINQ stands for Language Integrated Query. LINQ is a data querying API that provides querying capabilities khổng lồ .NET languages with a syntax similar to a SQL. LINQ queries use C# collections lớn return data.

Bạn đ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, & 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 data from any object that implements the IEnumerable interface.
The official goal of the LINQ family of technologies is to địa chỉ cửa hàng "general purpose query facilities to the .NET Framework that apply to lớn all sources of information, not just relational or XML data".
*

LINQ offers an object-based, language-integrated way lớn 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, like VB or C# in much the same way as you would query a database using SQL
LINQ khổng lồ Object provides functionality to lớn query in-memory objects & collections.Any class that implements the IEnumerable interface (in the System.Collections.Generic namespace) can be queried with SQO.

Xem thêm: Giải Địa Lý Lớp 10 Bài 9: Tác Nhân Nào Tạo Nên Các Loại Đá Nấm


LINQ lớn 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 lớn SQL (DLinq) Queries performed against the relation database only Microsoft SQL hệ thống Supported LINQ to lớn DataSet Supports queries by using ADO.NET data sets và data tables LINQ to lớn Entities
Here is a simple example that creates a array of integers. A LINQ query is used lớn 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 website page or UI with a GridView control that we will use to lớn 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 menu 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 khổng lồ query the collection.

Xem thêm: Đâu Là Tính Cách Phụ Nữ Thu Hút Đàn Ông Nhất Của Phụ Nữ, 9 Đặc Điểm Của Nữ Giới 'Hớp Hồn' Đàn Ông


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 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 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=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 list.", oddNumbers);