linq操作符:转换操作符

时间:2023-03-09 09:02:58
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的扩展方法。来看方法的定义:

 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 > );

二、ToArray操作符

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

 public T[] ToArray();

看下面的例子:

 List<int> list = new List<int>();
list.Add();
list.Add();
list.Add();
// 将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=,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-)},
new Category(){ Id=,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-)},
new Category(){ Id=,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-)},
new Category(){ Id=,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-)}
};
var dict= listCategory.ToDictionary(c => c.Id, c => c.CategoryName);
foreach(var item in dict)
{
Console.WriteLine($"key:{item.Key},value:{item.Value}");
}

结果:

linq操作符:转换操作符

注意:

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的类型:

linq操作符:转换操作符

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

linq操作符:转换操作符

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

四、ToList操作符

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

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

来看下面的例子:

 int[] intArray = { , , , , ,  };
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> elementSelector);
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 CreateTime { get; set; }
}
}

在方法中调用:

 List<Product> listProduct = new List<Product>()
{
new Product(){Id=,CategoryId=, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
new Product(){Id=,CategoryId=, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-)},
new Product(){Id=,CategoryId=, Name="活着", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="高等数学", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-)}
};
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}");
}
}

结果:

linq操作符:转换操作符

注意:

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值的类型:

linq操作符:转换操作符

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

linq操作符:转换操作符

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

六、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();
arrayList.Add();
arrayList.Add();
//非泛型转换成泛型
var list = arrayList.Cast<int>();
foreach (var item in list)
{
Console.WriteLine(item);
}

结果:

linq操作符:转换操作符

注意:

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

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

 ArrayList arrayList = new ArrayList();
arrayList.Add();
arrayList.Add("");
arrayList.Add();
//非泛型转换成泛型
var list = arrayList.Cast<int>();
foreach (var item in list)
{
Console.WriteLine(item);
}

程序运行结果:

linq操作符:转换操作符