C#學習日誌 day7 --------------LINQ與Lamda語句的初步嘗試以及XML的產生

來源:互聯網
上載者:User

標籤:

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的產生

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.