using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Linq1
{
public class linq2
{
public string firstname { get; set; }
public string lastname { get; set; }
public string emailaddress { get; set; }
public override string ToString()
{
return string.Format("{0} {1}\nemail: {2}", firstname, lastname, emailaddress);
}
}
class Program
{
static void Main(string[] args)
{
List<linq2> customers=createcustomerlist();
IEnumerable<linq2> result = from customer in customers where customer.firstname == "donna" select customer;
Console.WriteLine("firstname==\"donna\"");
foreach(linq2 customer in result)
{
Console.WriteLine(customer.ToString());
}
customers[3].firstname = "donna";//为什么这里赋值后,result这个变量也改了呢!是不是又自动执行了上面Ienumerable<linq2>这行; Console.WriteLine("firstname=\"donna\"(take two)");
foreach (linq2 customer in result)
{
Console.WriteLine(customer.ToString());
}
}
private static List<linq2> createcustomerlist()
{
List<linq2> customer5=new List<linq2>
{
new linq2{firstname="orlando",lastname="gee",emailaddress="[email protected]"},
new linq2{firstname="donna",lastname="carreras",emailaddress="[email protected]"}
,new linq2{firstname="bb166",lastname="qiu",emailaddress="[email protected]"}
,new linq2{firstname="jbqiu",lastname="qiujianbin",emailaddress="[email protected]"}
,new linq2{firstname="jbqiu168",lastname="qiujianbin168",emailaddress="[email protected]"}
};
return customer5;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Linq1
{
public class linq2
{
public string firstname { get; set; }
public string lastname { get; set; }
public string emailaddress { get; set; }
public override string ToString()
{
return string.Format("{0} {1}\nemail: {2}", firstname, lastname, emailaddress);
}
}
class Program
{
static void Main(string[] args)
{
List<linq2> customers=createcustomerlist();
IEnumerable<linq2> result = from customer in customers where customer.firstname == "donna" select customer;
Console.WriteLine("firstname==\"donna\"");
foreach(linq2 customer in result)
{
Console.WriteLine(customer.ToString());
}
customers[3].firstname = "donna";//为什么这里赋值后,result这个变量也改了呢!是不是又自动执行了上面Ienumerable<linq2>这行; Console.WriteLine("firstname=\"donna\"(take two)");
foreach (linq2 customer in result)
{
Console.WriteLine(customer.ToString());
}
}
private static List<linq2> createcustomerlist()
{
List<linq2> customer5=new List<linq2>
{
new linq2{firstname="orlando",lastname="gee",emailaddress="[email protected]"},
new linq2{firstname="donna",lastname="carreras",emailaddress="[email protected]"}
,new linq2{firstname="bb166",lastname="qiu",emailaddress="[email protected]"}
,new linq2{firstname="jbqiu",lastname="qiujianbin",emailaddress="[email protected]"}
,new linq2{firstname="jbqiu168",lastname="qiujianbin168",emailaddress="[email protected]"}
};
return customer5;
}
}
}
解决方案 »
- (急)Webservice的Content-Type问题
- C# 编写数据库同步问题
- networkstream beginread 非阻塞
- 不用模板页,写一个集成Page的类,要有模板页的效果
- 求颜色 各编码间的转换 这个RGB怎么获得
- 存储过程与DataGridView 高手过来指点下
- asp.net中dataset的使用
- 导入txt数据时,数据导完之后,提示数组越界,运行不到最后一步。不知道什么地方出了问题,求各位大神帮帮忙
- drowdownlist 默认选项的问题
- 如何取得程序当前目录?在线等待!!
- 一个Button样式的实现
- 如何对DataTable中的数据进行迭代显示在Label控件中?
每一次遍历linq查询表达式的结果,都会重复执行一次,每个查询表达式只有在真正对其运算的时候才会执行。
延迟执行(使用的时候才会执行): List<int> numbers = new List<int>() { 1, 2 };
IEnumerable<int> query = numbers.Select(i => i * 10);
numbers.Add(3);
//可见在linq查询表达式后才添加的“3”也在查询结果中
foreach (var item in query)
{
Console.WriteLine(item);//输出:10,20,30
}
重复执行(每一次调用查询表达式都会重复执行): List<int> numbers = new List<int>() { 1, 2 };
IEnumerable<int> query = numbers.Select(i => i * 10);
numbers.Add(3);
//可见在linq查询表达式后才添加的“3”也在查询结果中
foreach (var item in query)
{
Console.WriteLine(item);//输出:10,20,30
}
numbers.Clear();//清空numbers集合
//可见linq查询表达式又重新执行了一次
foreach (var item in query)
{
Console.WriteLine(item);//输出结果为空
}
IEnumerable<int> query = numbers.Select(i => i * 10).ToArray();
numbers.Add(3);
foreach (var item in query)
{
Console.WriteLine(item);//输出:10,20
} numbers.Clear();//清空numbers集合
foreach (var item in query)
{
Console.WriteLine(item);//输出:10,20
}
要避免linq查询表达式的这个特性可以在查询语句后调用一下方法语法的查询
查询表达式都有重复执行、延迟执行的特性,而方法语法就会立即执行。
这是我看Linq资料里提及相当频繁的一点.
IEnumerable<linq2> result = from customer in customers where customer.firstname == "donna" select customer;
修改成这样,就不会跟着变了:
List<linq2> result = (from customer in customers where customer.firstname == "donna" select customer).ToList();这不就是LINQ的延迟查询特性么...