C#学习日志 day7 --------------LINQ与Lamda语句的初步尝试以及XML的生成
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 = "orlando0@adventure-works.com"}, new Customer { FirstName = "Keith", LastName = "Harris",HomeAddress = "keith0@adventure-works.com" }, new Customer { FirstName = "Donna", LastName = "Carreras",HomeAddress = "donna0@adventure-works.com" }, new Customer { FirstName = "Janet", LastName = "Gates",HomeAddress = "janet1@adventure-works.com" }, new Customer { FirstName = "Lucy", LastName = "Harrington",HomeAddress = "lucy0@adventure-works.com" } }; 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>orlando0@adventure-works.com</emailAddress></customer> <customer> <firstName>Keith</firstName> <lastName>Harris</lastName> <emailAddress>keith0@adventure-works.com</emailAddress></customer> <customer> <firstName>Donna</firstName> <lastName>Carreras</lastName> <emailAddress>donna0@adventure-works.com</emailAddress> </customer> <customer> <firstName>Janet</firstName> <lastName>Gates</lastName> <emailAddress>janet1@adventure-works.com</emailAddress></customer> <customer> <firstName>Lucy</firstName> <lastName>Harrington</lastName> <emailAddress>lucy0@adventure-works.com</emailAddress></customer></customers>
这里的XmlElement firstElm = customerXml.CreateElement("firstName");语句是定义firstElm标签,这在html中是不行的
而xml的产生需要用到System.Xml.linq;命名空间。