第六次作业

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{

List<Customer> customers = CreateCustomerList(); // Find customer by first name
IEnumerable<Customer> result = from customer in customers//指定范围变量和数据源 customers

where customer.FirstName == "Donna" select customer;

List<Customer> cachedResult = result.ToList<Customer>();//如果你想缓存结果,这样它就可以加工后无需重新制作查询,

//你可以用ToList方法或ToArray()方法来保存结果的副本。

Console.WriteLine("FirstName == \"Donna\"");

foreach (Customer customer in result)

{

Console.WriteLine(customer.ToString());

}

customers[3].FirstName = "Donna";

Console.WriteLine("FirstName == \"Donna\" (take two)");

foreach (Customer customer in result)
{
Console.WriteLine(customer.ToString());
Console.ReadLine();
}//将第四个对象的First name改为Donna并输出
}


private static List<Customer> CreateCustomerList()

{

List<Customer> customers = new List<Customer>

{ new Customer

{ FirstName = "Orlando",LastName = "Gee", EmailAddress = "orlando0@adventure-works.com"},

new Customer { FirstName = "Keith", LastName = "Harris", EmailAddress = "keith0@adventure-works.com" },

new Customer { FirstName = "Donna", LastName = "Carreras", EmailAddress = "donna0@adventure-works.com" },

new Customer { FirstName = "Janet", LastName = "Gates", EmailAddress = "janet1@adventure-works.com" },

new Customer { FirstName = "Lucy", LastName = "Harrington", EmailAddress = "lucy0@adventure-works.com" } };

return customers;
}
}
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
// Overrides the Object.ToString() to provide a
// string representation of the object properties.
public override string ToString()
{
return string.Format("{0} {1}\nEmail: {2}",
FirstName, LastName, EmailAddress);
}
}

posted @ 2015-04-27 20:11    阅读(78)  评论(0编辑  收藏  举报