LINQ之非延迟执行标准查询操作符(上)
操作符:ToArray
原型:
public static TSource[] ToArray<TSource>( this IEnumerable<TSource> source )
描述:将一个IEnumerable<T>的输入序列,转换成T类型的数组,这个方法经常用来缓存一个序列,以防止在我们遍历它之前, 序列的数据发生改变。
例子:
List<Product> products = new List<Product> { new Product{ProductId = 201,ProductName = "Chuckit",CategoryId = 2}, new Product{ProductId = 202,ProductName = "SafeMade",CategoryId = 2}, new Product{ProductId = 101,ProductName = "Taste",CategoryId = 1}, new Product{ProductId = 102,ProductName = "Canidae",CategoryId = 1}, new Product{ProductId = 103,ProductName = "Flavor",CategoryId = 1} }; string [] productArray = products.Select(p => p.ProductName).ToArray(); foreach (string product in productArray) { Console.WriteLine(product); }
操作符:ToList
原型:
public static List<TSource> ToList<TSource>( this IEnumerable<TSource> source )
描述:将输入序列转换成T类型的List对象,同ToArray一样,这个方法经常用来缓存一个序列,以防止在我们遍历它之前, 序列的数据发生改变。
string[] allBrands = new string[] { "Exuviance", "Avene", "Baby Quasar", "Ecoya", "Alterna", "Ecru New York" }; List<int> brandLength = allBrands.Select(b => b.Length).ToList(); foreach (int length in brandLength) { Console.WriteLine(length); }
操作符:ToDictonary
原型:四种
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector )
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer )
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector )
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer )
描述:根据指定的key,返回一个Dictionary序列
原型一和原型二,原型三和原型四,提供了IEqualityComparer用于比较2个对象是否相等,原型一和原型三,主要区别在于与原型三允许返回与输入序列不一样类型的序列,
因此,我们主要看看原型一。
List<Product> products = new List<Product> { new Product{ProductId = 201,ProductName = "Chuckit",CategoryId = 2}, new Product{ProductId = 202,ProductName = "SafeMade",CategoryId = 2}, new Product{ProductId = 101,ProductName = "Taste",CategoryId = 1}, new Product{ProductId = 102,ProductName = "Canidae",CategoryId = 1}, new Product{ProductId = 103,ProductName = "Flavor",CategoryId = 1} }; Dictionary<int, Product> productDic = products.ToDictionary(p => p.ProductId); Product someProduct = productDic[201]; Console.WriteLine("{0},{1}",someProduct.ProductId,someProduct.ProductName);
操作符:ToLookup
原型:ToLookup和ToDictionary一样提供了四种原型,且四种都是对应的。
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector )
描述:ToLookup返回的是一个ILookup类型的序列,ILookup允许我们根据key来查找,跟Dictionary不一样的地方是,key可以不唯一,且多个元素可以存在一个key里面
List<Product> products = new List<Product> { new Product{ProductId = 201,ProductName = "Chuckit",CategoryId = 2}, new Product{ProductId = 202,ProductName = "SafeMade",CategoryId = 2}, new Product{ProductId = 101,ProductName = "Taste",CategoryId = 1}, new Product{ProductId = 102,ProductName = "Canidae",CategoryId = 1}, new Product{ProductId = 103,ProductName = "Flavor",CategoryId = 1} }; ILookup<int, Product> productLookups = products.ToLookup(p => p.CategoryId); IEnumerable < Product > productLookup = productLookups[1]; foreach (Product product in productLookup) { Console.WriteLine("{0},{1}",product.ProductId,product.ProductName); }
在这个例子中,通过key在ILookup序列里面查找,返回的是一个IEnumerable<Product>的序列,而不再像Dictionary一样,返回唯一的一个对象。
操作符:SequenceEqual
原型:
public static bool SequenceEqual<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second )
public static bool SequenceEqual<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer )
描述:判断2个序列是否相等,注意,这里的相等,要求两个序列的元素个数要相等,相同索引位置的元素也要相等,原型二提供了相等比较器,允许自定义对象相等的算法。
来看个原型二的例子:
首先定义相等比较器:
public class StringNumberComparee:IEqualityComparer<string> { public bool Equals(string x, string y) { return x.TrimStart('0').Equals(y.TrimStart('0')); } public int GetHashCode(string obj) { return obj.GetHashCode(); } }
string[] productIds = new string[] { "1", "3", "5", "12", "34", "21" }; string[] productIds2 = new string[] { "0001", "0003", "0005", "0012", "0034", "0021" }; bool twoProductIdsAreSame = productIds.SequenceEqual(productIds2, new StringNumberComparee()); Console.WriteLine(twoProductIdsAreSame);
返回结果正如我们预料的为True。