LINQ (language-integrated Query) is a language pattern used as a lookup, Access database, or XML file.
Is the way to communicate object-oriented languages and databases. very similar to SQL.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;//LINQ namespacenamespaceprogramming_csharp{//Simple Customer Class Public classCustomer//Define a Customer object that forms the collection to search { Public stringFirstName {Get;Set; } Public stringLastName {Get;Set; } Public stringEmailAddress {Get;Set; } //Overrides the object.tostring () to provide a//string representation of the object properties. Public Override stringToString () {return string. Format ("{0} {1}\nemail: {2}", FirstName, LastName, EmailAddress); } }
//Create a customer list with sample dataPrivate StaticList<customer>createcustomerlist () {List<Customer> customers =NewList<customer> { NewCustomer {FirstName ="Orlando ", LastName ="Gee",EmailAddress ="[email protected]"}, NewCustomer {FirstName ="Keith ", LastName ="Harris",EmailAddress ="[email protected]" }, NewCustomer {FirstName ="Donna ", LastName ="Carreras",EmailAddress ="[email protected]" }, NewCustomer {FirstName ="Janet ", LastName ="Gates",EmailAddress ="[email protected]" }, NewCustomer {FirstName ="Lucy ", LastName ="Harrington",EmailAddress ="[email protected]" } }; returncustomers; }
An in-memory collection that makes up the customer
Static voidMain ()//Main Program{List<Customer> customers =createcustomerlist (); //Find Customer by First nameienumerable<customer> result = fromCustomerinchCustomerswhereCustomer. FirstName = ="Donna" SelectCustomer//Select on the lastConsole.WriteLine ("FirstName = = \ "Donna\""); foreach(Customer Customerinchresult) {Console.WriteLine (Customer. ToString ());} //Show Resultscustomers[3]. FirstName ="Donna";//Modifying DataConsole.WriteLine ("FirstName = = \ "Donna\" ( take both)");//Show Results foreach(Customer Customerinchresult) {Console.WriteLine (Customer. ToString ());} } }}//namespace
Join keyword
Connect two data tables connected by the HYPHEN keyword
[Data source 1] Join [data source 2] on [join condition]
LINQ contains Keywords
Where from select and so on
A Where expression filter condition similar to SQL
From indicates the specified range
Select indicates lookup mappings
var keyword
The var universal data type must be initialized
var 1234 ; var " 1234 " ; var New Class1;
C # LINQ & XML