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的生成的更多相关文章

  1. LINQ to SQL语句非常详细(原文来自于网络)

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...

  2. 【转】LINQ to SQL语句(1)之Where

    Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句. Where操作包括3种形式,分别为简单形 ...

  3. Entity Framework学习笔记(四)----Linq查询(1)

    请注明转载地址:http://www.cnblogs.com/arhat 从本章开始,老魏就介绍一下Entity Framework使用Linq来查询数据,也就是Linq To Entity.其实在E ...

  4. LINQ简介和LINQ to SQL语句之Where

    LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性.已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感 ...

  5. 算法面试题:一个List<Student>,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!

    算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点! 解题思路 这是群里某位小伙伴去面试碰到的面试题,从题目本身来看,面试官应该是要考察面试者对泛型 Lis ...

  6. LINQ to SQL语句(7)之Exists/In/Any/All/Contains

    适用场景:用于判断集合中元素,进一步缩小范围. Any 说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).有2种形式,分别为简单形式 ...

  7. 年终巨献 史上最全 ——LINQ to SQL语句

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  8. LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods

    适用场景:适量的取出自己想要的数据,不是全部取出,这样性能有所加强. Take 说明:获取集合的前n个元素:延迟.即只返回限定数量的结果集. var q = ( from e in db.Employ ...

  9. SQL、Linq、lamda表达式 同一功能不同写法

    一.SQL.Linq.lamda表达式 同一功能不同写法 SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employe ...

随机推荐

  1. Oracle的关于建表,约束,查询等的练习

    从建立一个简单表,到实现一些复杂查询的例子, DROP TABLE grade;DROP TABLE item;DROP TABLE sporter;CREATE TABLE sporter( spo ...

  2. 成本卷积报错:CSTPSCEX.explode_sc_cost_flags():40:ORA-01476: 除数为 0

    成本卷积请求:供应链成本累计 - 打印报表 运行后报一下错误: MSG-00000: Rollup ID = 236403MSG-00000: Before CSTPSCEX.supply_chain ...

  3. c#中的数据类型简介

    一.C#中的变量和常量 C#中用于定义常量的方式有两种一个使用const关键字,一个是用readonly关键字.使用const定义的常量叫静态常量(compile-time constant),用re ...

  4. 安装MYSQL出现的问题

    安装MYSQL接近三天了还是没有安装好,原因1:我觉得错误日志产生的原因是因为 计算机的名称是中文名 :小石头 瓦卡卡,果然是这个原因,折腾了三四天,最后换了系统!!!!把计算机名命名成了英文,果然一 ...

  5. C语言常用的库文件(头文件、函数库)

    C语言常用的库文件(头文件.函数库) C系统提供了丰富的系统文件,称为库文件.C的库文件分为两类,一类是扩展名为".h"的文件,称为头文件,在前面的包含命令中我们已多次使用过.在& ...

  6. android 向SD卡写入数据

    原文:android 向SD卡写入数据 1.代码: /** * 向sdcard中写入文件 * @param filename 文件名 * @param content 文件内容 */ public v ...

  7. 在linux下读取bmp文件头的完整代码。

    呵呵,贴在这里记录一下. [cpp] view plaincopy #include<stdio.h> #include<string.h> #include<sys/t ...

  8. 修改TOMCAT服务器图标为应用LOGO

    在tomcat下部署应用程序,运行后,发现在地址栏中会显示tomcat的小猫咪图标.有时候,我们自己不想显示这个图标,想换成自己定义的的图标,那么按如下方法操作即可: 参考网上的解决方案:1.将$TO ...

  9. Aix_bugzilla

    原创作品,转载请注明出处! Bugzilla在AIX上部署,网上看到的不多.我耗费了很长时间才算部署完,记录在这里,以防忘记了. 一.    下载安装文件或源代码 1. 下载Bugzilla 3.6. ...

  10. 多校联合练习赛1 Problem1005 Deque LIS+LDS 再加一系列优化

    Deque Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...