linq操作符:转换操作符

这些转换操作符将集合转换成数组: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的扩展方法。来看方法的定义:

1 public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)

 看看下面的例子:

1 DataTable dt = new DataTable();
2 // 将dt先使用AsEnumerable()操作符进行转换,然后在调用Linq to Object 的where方法
3 var list= dt.AsEnumerable().Where(p => p.Name.length > 0);

 二、ToArray操作符

ToArray操作符可以在IEnumerable<T>类型的任何派生对象上调用,返回值为T类型的数组。其方法定义如下:

1 public T[] ToArray();

 看下面的例子:

1 List<int> list = new List<int>();
2 list.Add(1);
3 list.Add(3);
4 list.Add(4);
5 // 将List<int>类型的集合转换成int[]数组
6 int[] intArray = list.ToArray();

三、ToDictionary操作符

ToDictionary操作符根据指定的键选择器函数,从IEnumerable<T>创建一个Dictionary<TKey,TValue>。

开看下面的例子。

先定义Category类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConvertOperation
 8 {
 9     public class Category
10     {
11         public int Id { get; set; }
12         public string CategoryName { get; set; }
13         public DateTime CreateTime { get; set; }
14     }
15 }

 调用:

 1 List<Category> listCategory = new List<Category>()
 2 {
 3         new Category(){ Id=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)},
 4         new Category(){ Id=2,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)},
 5         new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
 6         new Category(){ Id=4,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-34)}
 7 };
 8 var dict= listCategory.ToDictionary(c => c.Id, c => c.CategoryName);
 9 foreach(var item in dict)
10 {
11         Console.WriteLine($"key:{item.Key},value:{item.Value}");
12 }

 结果:

注意:

1、如果省略ToDictionary()方法的第二个参数(值选择函数),那么value将会保存一个类别对象。看下面的例子:

1 var dict = listCategory.ToDictionary(c=>c.Id);
2 foreach (var item in dict)
3 {
4     Console.WriteLine($"key:{item.Key},Id:{dict[item.Key].Id},CategoryName:{dict[item.Key].CategoryName},CreateTime:{dict[item.Key].CreateTime}");               
5 }

在程序运行时打断点,查询value的类型:

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

2、如果key值为null或者出现重复的key,那么将会导致程序抛出异常。(字典的key值不可以是重复的)

四、ToList操作符

ToList操作符可以在IEnumerable<T>类型的任何派生对象上使用,返回值是List<T>类型的集合。其定义如下:

1 public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);

 来看下面的例子:

1 int[] intArray = { 1, 2, 3, 56, 78, 34 };
2 List<int> list = intArray.ToList();

 五、ToLookUp操作符

ToLookUp操作符将创建一个LookUp<TKey,TElement>对象,这是一个one-to-many的集合,一个key可以对应多个value值。其方法定义如下:

1 public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
2 public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, 
Func<TSource, TElement> elementSelector); 3 public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer); 4 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可以对应多个产品,其定义如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConvertOperation
 8 {
 9     public class Product
10     {
11         public int Id { get; set; }
12         public int CategoryId { get; set; }
13         public string Name { get; set; }
14         public double Price { get; set; }
15         public DateTime CreateTime { get; set; }
16     }
17 }

 在方法中调用:

 1 List<Product> listProduct = new List<Product>()
 2 {
 3       new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
 4       new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
 5       new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
 6       new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
 7       new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
 8 };
 9 var list = listProduct.ToLookup(p => p.CategoryId, p => p.Name);
10 foreach (var item in list)
11 {
12       Console.WriteLine($"key:{item.Key}");
13       foreach (var p in item)
14       {
15           Console.WriteLine($"value:{p}");
16       }
17 }

结果:

注意:

1、如果省略ToLookUp()方法的第二个参数(值选择函数),那么value将会保存一个类别对象。看下面的例子: 

1 var list1 = listProduct.ToLookup(p => p.CategoryId);
2 foreach (var item in list1)
3 {
4       Console.WriteLine($"key:{item.Key}");
5       foreach (var p in item)
6       {
7           Console.WriteLine($"Id:{p.Id},CategoryId:{p.CategoryId},Name:{p.Name},CreateTime:{p.CreateTime}");
8       }
9 }

 程序运行时打断点,查看value值的类型:

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

2、ToLookUp和GroupBy操作很相似,只不过GroupBy是延迟加载的,ToLookUp是立即加载的。

六、Cast操作符

Cast操作符用于将一个类型为IEnumerable的集合对象转换为IEnumerable<T>类型的集合对象。也就是非泛型集合转成泛型集合,因为在Linq to OBJECT中,绝大部分操作符都是针对IEnumerable<T>类型进行的扩展方法。因此对非泛型集合并不适用。来看方法定义:

1 public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source);

 来看下面的例子:

 1 ArrayList arrayList = new ArrayList();
 2 arrayList.Add(1);
 3 arrayList.Add(2);
 4 arrayList.Add(3);
 5 //非泛型转换成泛型
 6 var list = arrayList.Cast<int>();
 7 foreach (var item in list)
 8 {
 9      Console.WriteLine(item);
10 }

 结果:

注意:

1、使用Cast()方法时必须要传入类型参数。

2、序列中的元素必须要能转换为类型 TResult。看下面的例子:

 1 ArrayList arrayList = new ArrayList();
 2 arrayList.Add(1);
 3 arrayList.Add("2");
 4 arrayList.Add(3);
 5 //非泛型转换成泛型
 6 var list = arrayList.Cast<int>();
 7 foreach (var item in list)
 8 {
 9      Console.WriteLine(item);
10 }

程序运行结果:

 

原文地址:https://www.cnblogs.com/dotnet261010/p/9311868.html