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;命名空间。
C#学习日志 day7 --------------LINQ与Lamda语句的初步尝试以及XML的生成的更多相关文章
- LINQ to SQL语句非常详细(原文来自于网络)
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...
- 【转】LINQ to SQL语句(1)之Where
Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形 ...
- Entity Framework学习笔记(四)----Linq查询(1)
请注明转载地址:http://www.cnblogs.com/arhat 从本章开始,老魏就介绍一下Entity Framework使用Linq来查询数据,也就是Linq To Entity.其实在E ...
- LINQ简介和LINQ to SQL语句之Where
LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性.已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感 ...
- 算法面试题:一个List<Student>,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!
算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点! 解题思路 这是群里某位小伙伴去面试碰到的面试题,从题目本身来看,面试官应该是要考察面试者对泛型 Lis ...
- LINQ to SQL语句(7)之Exists/In/Any/All/Contains
适用场景:用于判断集合中元素,进一步缩小范围. Any 说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).有2种形式,分别为简单形式 ...
- 年终巨献 史上最全 ——LINQ to SQL语句
LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...
- LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods
适用场景:适量的取出自己想要的数据,不是全部取出,这样性能有所加强. Take 说明:获取集合的前n个元素:延迟.即只返回限定数量的结果集. var q = ( from e in db.Employ ...
- SQL、Linq、lamda表达式 同一功能不同写法
一.SQL.Linq.lamda表达式 同一功能不同写法 SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employe ...
随机推荐
- iOS设计模式解析(一)工厂方法
工厂方法:定义创建对象的借口,让子类决定实例化哪一个类.工厂方法是一个类的实例化延迟到了子类 例如 :Shoes厂有两个子类(Newbalance.Nike)构建类图如下: 代码实现: #i ...
- JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- hdu1251统计难题
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- 使用Open Live Writer 的代码高亮插件体验
由于windows live writer 2012 已经停止服务,转而推出开源项目Open Live Writer .虽然Open Live Writer 也没怎么更新,官网更是一个插件都没有放出来 ...
- [Python] 应用kNN算法预测豆瓣电影用户的性别
应用kNN算法预测豆瓣电影用户的性别 摘要 本文认为不同性别的人偏好的电影类型会有所不同,因此进行了此实验.利用较为活跃的274位豆瓣用户最近观看的100部电影,对其类型进行统计,以得到的37种电影类 ...
- knockout 与checkbox联动
knockout 通过teplate实现简单的代码实现复杂的操作绑定checkbox,代码如下自我感觉很赞!!! 前台HTml <ul data-bind="template: { n ...
- 4x4矩阵键盘扫描
4x4矩阵键盘扫描 Windows 10 IoT Core 是微软针对物联网市场的一个重要产品,与以往的Windows版本不同,是为物联网设备专门设计的,硬件也不仅仅限于x86架构,同时可以在ARM架 ...
- Oracle EBS-SQL (WIP-12):总装车间任务查询.sql
select WT.WIP_ENTITY_NAME 任务名 ...
- 项目与软件推荐之编辑器-QOwnNotes(刺激自己)
项目与软件推荐之编辑器-QOwnNotes 今天推荐一款软件 QOwnNotes,是一款普通文本笔记软件.以某个路径为目录,罗列出目录下所有的 md 文件或者 txt 文件. 有如下亮点: 启动速度快 ...
- Android 微信分享信息
随着微信越来越火,越来越多的应用要求有分享到微信的功能.虽然有很多平台都帮集成有分享功能,比如友盟.但是个人觉得友盟集成的东西太多了,自己封装得太过分了,很多资源文件也要带进去,所以感觉不是怎么好,所 ...