LINQ(Language-INtegrated Query) 是一種用做查找、存取數據庫或XML文件的語言模式。數據庫
是溝通面嚮對象語言與數據庫的方式。與SQL很類似。ide
using System; using System.Collections.Generic; using System.Linq; //linq namespace namespace Programming_CSharp { // Simple customer class public class Customer //定義customer對象,構成要搜索的集合 { public string FirstName { get; set; } public string LastName { get; set; } public string EmailAddress { get; set; } // Overrides the Object.ToString() to provide a // string representation of the object properties. public override string ToString() { return string.Format("{0} {1}\nEmail: {2}", FirstName, LastName, EmailAddress); } }
// Create a customer list with sample data private static List<Customer> CreateCustomerList() { List<Customer> customers = new List<Customer> { new Customer { FirstName = "Orlando「,LastName = "Gee", EmailAddress = "orlando0@adventure-works.com"}, new Customer { FirstName = "Keith「, LastName = "Harris", EmailAddress = "keith0@adventure-works.com" }, new Customer { FirstName = "Donna「, LastName = "Carreras", EmailAddress = "donna0@adventure-works.com" }, new Customer { FirstName = "Janet「, LastName = "Gates", EmailAddress = "janet1@adventure-works.com" }, new Customer { FirstName = "Lucy「, LastName = "Harrington", EmailAddress = "lucy0@adventure-works.com" } }; return customers; }
// 內存中構成Customer的集合spa
static void Main() // Main program { List<Customer> customers = CreateCustomerList(); // Find customer by first name IEnumerable<Customer> result = from customer in customers where customer.FirstName == "Donna" select customer; //select 放在最後 Console.WriteLine("FirstName == \"Donna\""); foreach (Customer customer in result) { Console.WriteLine(customer.ToString());} //顯示結果 customers[3].FirstName = "Donna"; //修改數據 Console.WriteLine("FirstName == \"Donna\" (take two)"); //顯示結果 foreach (Customer customer in result) { Console.WriteLine(customer.ToString());} } } }//namespace
Join 關鍵字code
內連關鍵字 將兩個數據錶鏈接鏈接orm
[data source 1] join [data source 2] on [join condition]對象
LINQ中含有關鍵字blog
where from select等內存
與SQL相似的 where表示篩選條件get
from 表示指定的範圍string
select 表示查找映射
Var關鍵字
var通用數據類型 必須初始化
var num = 1234; var strnum = "1234"; var classsimple = new class1;