using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 立即执行的Enumerable类方法成员
{
class Program
{
static void Main(string[] args)
{
//1.ToArray序列转换成数组
List<string> names =new List<string> { "DebugLZQ","Sarah","Jerry","Jeffrey","M&M"};
string[] takenames = names.ToArray();
string[] takenames2 = (from name in names
where name.IndexOf("Je")>-1
select name).ToArray();
//2.ToList序列转换成List<T>
string[] namesA = { "DebugLZQ", "Sarah", "Jerry", "Jeffrey", "M&M" };
List<string> takenames_ToList = namesA.ToList();
List<string> takenames_ToList2 = (from name in namesA select name).ToList();
//
}
}
}
程序结果显而易见,所以没有写输出语句;
3.ToDictionary把序列转换为泛型Dictionary<TKey,TValue>
4.ToLookup用于将序列转换为泛型Lookup<TKey,TValue>
Dictionary和Lookup是非常近似的一对类型,都通过“键”访问相关的元素,不同的是Dictionary的Key和Value是一一对应关系,Lookup的Key和Value是一对多关系,Lookup没有公共构造函数,时能用ToLookup构建,创建后也不能删除Lookup中的元素。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ToDictionary
{
/// <summary>
/// 3.ToDictionary把序列转换为泛型Dictionary<TKey,TValue>
/// </summary>
class Program
{
static void Main(string[] args)
{
List<GuestInfo> gList = new List<GuestInfo>()
{
new GuestInfo(){Name="Jeffrey", Age=33,Tel="136********"},
new GuestInfo(){ Name="DebugLZQ", Age=25,Tel="187********"},
new GuestInfo(){Name="Sarah", Age=24,Tel="159********"},
new GuestInfo(){Name="Jerry", Age=33,Tel="135********"},
new GuestInfo(){Name="Smith", Age=33,Tel="139********"}
};
//ToDictionary把序列转换为泛型Dictionary
//ToDictionary重载了4个方法
//a.用Name作为Dictionary的“键”,guest为“value”
Dictionary<string, GuestInfo> dictionary1 = gList.ToDictionary(guest => guest.Name);
foreach (var s in dictionary1 )
{
Console.WriteLine("键值{0}:{1} {2} {3}",s.Key,s.Value.Name,s.Value.Age,s.Value.Tel );
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
//b.自定义比较器
Dictionary<string,GuestInfo> dictionary2=gList.ToDictionary(guest=>guest.Name,new MyEqualityComparer<string>());
foreach (var s in dictionary2)
{
Console.WriteLine("键值{0}:{1} {2} {3}", s.Key, s.Value.Name, s.Value.Age, s.Value.Tel);
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
//c.用Name作为Dictionary的“键”,Tel属性为"value"
Dictionary<string, string> dictionary3 = gList.ToDictionary(guest=>guest.Name,g=>g.Tel);
foreach (var s in dictionary3)
{
Console.WriteLine("键值{0}:{1}", s.Key, s.Value);
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
//d.自定义比较器
Dictionary<string, string> dictionary4 = gList.ToDictionary(guest=>guest.Name,g=>g.Tel,new MyEqualityComparer<string>());
foreach (var s in dictionary4)
{
Console.WriteLine("键值{0}:{1}", s.Key, s.Value);
}
Console.WriteLine("------------------------------------------------------");
Console.ReadKey();
///////////////
///4.ToLookup用于将序列转换为泛型Lookup<TKey,TValue>。
///Dictionary和Lookup是非常近似的一对类型,都通过“键”访问相关的元素,不同的是Dictionary的Key和Value是一一对应关系
///Lookup的Key和Value是一对多关系
///Lookup没有公共构造函数,时能用ToLookup构建,创建后也不能删除Lookup中的元素。
///该方法也有4个原型,和上面的ToDictionary极像
///
//a. Name的第一个字符(字符串)作key
ILookup<string, GuestInfo> lookup1 = gList.ToLookup(guest => guest.Name.Substring(0, 1));
foreach (var k in lookup1)
{
Console.WriteLine(k.Key);//键值
foreach (var v in k)
{
Console.Write("{0},{1},{2}",v.Name,v.Age,v.Tel );
}
Console.WriteLine();
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
//b自定义比较器
ILookup<string, GuestInfo> lookup2 = gList.ToLookup(guest => guest.Name.Substring(0, 1), new MyEqualityComparer<string>());
foreach (var k in lookup2)
{
Console.WriteLine(k.Key);//键值
foreach (var v in k)
{
Console.Write("{0},{1},{2}", v.Name, v.Age, v.Tel);
}
Console.WriteLine();
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
//c
ILookup<string, string> lookup3 = gList.ToLookup(guest=>guest.Name.Substring(0,1),g=>g.Name );
foreach (var k in lookup3)
{
Console.WriteLine(k.Key);//键值
foreach (var v in k)
{
Console.Write("{0} ", v);
}
Console.WriteLine();
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
//d自定义比较器
ILookup<string, string> lookup4 = gList.ToLookup(guest=>guest.Name.Substring(0,1),g=>g.Name,new MyEqualityComparer<string>());
foreach (var k in lookup4)
{
Console.WriteLine(k.Key);//键值
foreach (var v in k)
{
Console.Write("{0} ", v);
}
Console.WriteLine();
}
Console.WriteLine("--------------------------------");
Console.ReadKey();
}
}
}










