目录
一、AsEnumerable操作符二、ToArray操作符三、ToDictionary操作符四、ToList操作符五、ToLookUp操作符六、Cast操作符这些转换操作符将集合转换成数组:IEnumerable、IList、IDictionary等。转换操作符是用来实现将输入对象的类型转变为序列的功能。名称以"As"开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合。名称以"To"开头的方法可枚举(即时加载)源集合并将项放入相应的集合类型。
一、AsEnumerable操作符
所有实现了IEnumerable<T>接口的类型都可以调用此方法来获取一个IEnumerable<T>集合。AsEnumerable操作符可以将一个类型为IEnumerable<T>的输入序列转换成一个IEnumerable<T>的输出序列,其主要用于将一个实现了IEnumerable<T>接口的对象转换成一个标准的IEnumerable<T>接口对象。在Linq中,不同领域的Linq实现都有自己专属的操作符。
例如:IQueryable<T>通常是Linq to SQL的返回类型,当我们之间在上面调用Where<>方法时,调用的是Linq to SQL的扩展方法,因此有时候我们需要转换为标准的IEnumerable<T>,然后在调用Linq to OBJECT的扩展方法。来看方法的定义:
public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
看看下面的例子:
DataTable dt = new DataTable();// 将dt先使用AsEnumerable()操作符进行转换,然后在调用Linq to Object 的where方法var list= dt.AsEnumerable().Where(p => p.Name.length > 0);
二、ToArray操作符
ToArray操作符可以在IEnumerable<T>类型的任何派生对象上调用,返回值为T类型的数组。其方法定义如下:
public T[] ToArray();
看下面的例子:
List<int> list = new List<int>();list.Add(1);list.Add(3);list.Add(4);// 将List<int>类型的集合转换成int[]数组int[] intArray = list.ToArray();
三、ToDictionary操作符
ToDictionary操作符根据指定的键选择器函数,从IEnumerable<T>创建一个Dictionary<TKey,TValue>。
开看下面的例子。
先定义Category类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConvertOperation{ public class Category { public int Id { get; set; } public string CategoryName { get; set; } public DateTime CreateTime { get; set; } }}调用:
List<Category> listCategory = new List<Category>(){ new Category(){ Id=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)}, new Category(){ Id=2,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)}, new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)}, new Category(){ Id=4,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-34)}};var dict= listCategory.ToDictionary(c => c.Id, c => c.CategoryName);foreach(var item in dict){ Console.WriteLine($"key:{item.Key},value:{item.Value}");}结果:

注意:
1、如果省略ToDictionary()方法的第二个参数(值选择函数),那么value将会保存一个类别对象。看下面的例子:
var dict = listCategory.ToDictionary(c=>c.Id);foreach (var item in dict){ Console.WriteLine($"key:{item.Key},Id:{dict[item.Key].Id},CategoryName:{dict[item.Key].CategoryName},CreateTime:{dict[item.Key].CreateTime}");}在程序运行时打断点,查询value的类型:

从截图中可以看出:这时value的类型是Category类型。其输出结果如下:

四、ToList操作符
ToList操作符可以在IEnumerable<T>类型的任何派生对象上使用,返回值是List<T>类型的集合。其定义如下:
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);
来看下面的例子:
int[] intArray = { 1, 2, 3, 56, 78, 34 };List<int> list = intArray.ToList();五、ToLookUp操作符
ToLookUp操作符将创建一个LookUp<TKey,TElement>对象,这是一个one-to-many的集合,一个key可以对应多个value值。其方法定义如下:
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elemuQUyoVentSelector);public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer);public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer);
从方法定义中可以看出:ToLookUp的value值的类型和集合中元素的类型一致。如果一个key对应多个value值,那么value会是TSource类型的集合。
来看下面的例子。
先定义Product类,Product类中的分类ID可以对应多个产品,其定义如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConvertOperation{ public class Product { public int Id { get; set; } public int CategoryId { get; set; } public string Name { get; set; } public double Price { get; set; } public DateTime CreateTimuQUyoVe { get; set; } }}在方法中调用:
List<Product> listProduct = new List<Product>(){ new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}};var list = listProduct.ToLookup(p => p.CategoryId, p => p.Name);foreach (var item in list){ Console.WriteLine($"key:{item.Key}"); foreach (var p in item) { Console.WriteLine($"value:{p}"); }}结果:

注意:
1、如果省略ToLookUp()方法的第二个参数(值选择函数),那么value将会保存一个类别对象。看下面的例子:
var list1 = listProduct.ToLookup(p => p.CategoryId);foreach (var item in list1){ Console.WriteLine($"key:{item.Key}"); foreach (var p in item) { Console.WriteLine($"Id:{p.Id},CategoryId:{p.CategoryId},Name:{p.Name},CreateTime:{p.CreateTime}"); }}程序运行时打断点,查看value值的类型:

从上面的截图中能够看出:这时value的类型是Product类型。运行结果如下:

六、Cast操作符
Cast操作符用于将一个类型为IEnumerable的集合对象转换为IEnumerable<T>类型的集合对象。也就是非泛型集合转成泛型集合,因为在Linq to OBJECT中,绝大部分操作符都是针对IEnumerable<T>类型进行的扩展方法。因此对非泛型集合并不适用。来看方法定义:
public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source);
来看下面的例子:
ArrayList arrayList = new ArrayList();arrayList.Add(1);arrayList.Add(2);arrayList.Add(3);//非泛型转换成泛型var list = arrayList.Cast<int>();foreach (var item in list){ Console.WriteLine(item);}结果:

注意:
1、使用Cast()方法时必须要传入类型参数。2、序列中的元素必须要能转换为类型 TResult。看下面的例子:ArrayList arrayList = new ArrayList();arrayList.Add(1);arrayList.Add("2");arrayList.Add(3);//非泛型转换成泛型var list = arrayList.Cast<int>();foreach (var item in list){ Console.WriteLine(item);}程序运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。








