標籤:
LINQ是一種整合在電腦語言裡的資訊查詢語句,是c#3.0中最惹人矚目的功能。
在C#中,LINQ語句有兩種寫法。
第一種寫法與SQL語句類似:
IEnumerable<Customer> result = from customer in customers where customer.FirstName == "Donna“ select customer;
第二種寫法更加接近c#語句:
IEnumerable<Customer> result = customers.Where(customer => customer.FirstName == "Donna") .Select(customer => customer);
這種寫法易於理解,所以我認為這種寫法更加好。
在Where和Select後面填入的是Lamda語句,這種語句是Delegate的簡化,有利於提升代碼的閱讀性。
Lamda運算式的形式通常是這樣的
people=>people.age>30
第一個people指的是傳入的參數, =>是Lamda運算式的特定符號,右邊是一個運算式,在查詢語句中,此符號後面的運算式傳回值通常是布爾類型的。例如上面這條語句放在Where中可以篩選出年齡大於三十的人。
下面是一個簡單的LINQ和Lambda運算式的運用
customer類:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{ public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string HomeAddress { get; set; } //now override the ToString function of Object class. public override string ToString() { return string.Format("{0} {1}\n Enmail:{2}", FirstName, LastName, HomeAddress); } public static List<Customer> CreateCustomerList() { List<Customer> customers = new List<Customer> { new Customer { FirstName = "Orlando",LastName = "Gee", HomeAddress = "[email protected]"}, new Customer { FirstName = "Keith", LastName = "Harris",HomeAddress = "[email protected]" }, new Customer { FirstName = "Donna", LastName = "Carreras",HomeAddress = "[email protected]" }, new Customer { FirstName = "Janet", LastName = "Gates",HomeAddress = "[email protected]" }, new Customer { FirstName = "Lucy", LastName = "Harrington",HomeAddress = "[email protected]" } }; return customers; } }}
在main函數中查詢以D開頭的記錄
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{ class Program { public static void Main() { List<Customer> customers = Customer.CreateCustomerList(); IEnumerable<Customer> result = customers.Where(customer => customer.FirstName.StartsWith("D")); foreach (Customer customer in result) { Console.WriteLine(customer.ToString()); } } }}
關於Xml我用上一個資料庫簡單的建立了一個xml文檔
public static void Main() { List<Customer> customers = Customer.CreateCustomerList(); XmlDocument customerXml = new XmlDocument(); XmlElement rootElem = customerXml.CreateElement("customers"); customerXml.AppendChild(rootElem); foreach (Customer cust in customers) { XmlElement customerElm = customerXml.CreateElement("customer"); XmlElement firstElm = customerXml.CreateElement("firstName"); firstElm.InnerText = cust.FirstName; customerXml.AppendChild(firstElm); XmlElement second = customerXml.CreateElement("lastName"); second.InnerText = cust.LastName; customerXml.AppendChild(second); XmlElement third = customerXml.CreateElement("emailAddress"); third.InnerText = cust.Address; customerXml.AppendChild(third); rootElem.AppendChild(customerElm); } Console.WriteLine(customerXml.OuterXml); }
運行結果是這樣的
<customers><customer><firstName>Orlando</firstName><lastName>Gee</lastName><emailAddress>[email protected]</emailAddress></customer><customer><firstName>Keith</firstName><lastName>Harris</lastName><emailAddress>[email protected]</emailAddress></customer><customer><firstName>Donna</firstName><lastName>Carreras</lastName><emailAddress>[email protected]</emailAddress></customer><customer><firstName>Janet</firstName><lastName>Gates</lastName><emailAddress>[email protected]</emailAddress></customer><customer><firstName>Lucy</firstName><lastName>Harrington</lastName><emailAddress>[email protected]</emailAddress></customer></customers>
這裡的XmlElement firstElm = customerXml.CreateElement("firstName");語句是定義firstElm標籤,這在html中是不行的
而xml的產生需要用到System.Xml.linq;命名空間。
C#學習日誌 day7 --------------LINQ與Lamda語句的初步嘗試以及XML的產生