zoukankan      html  css  js  c++  java
  • LINQ中转换操作符(十三)

    这些转换操作符将集合转换成数组:IEnumerable、IList、IDictionary等。转换操作符是用来实现将输入对象的类型转变为序列的功能。名称以"As"开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合。名称以"To"开头的方法可枚举(即时加载)源集合并将项放入相应的集合类型

    一、AsEnumerable操作符

     所有实现了IEnumerable<T>接口的类型都可以调用此方法来获取一个IEnumerable<T>集合

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
                DataRow dr = null;
    
                //DataTable添加列
                dt.Columns.Add("UserID", typeof(String));  //等效于dt.Columns.Add("UserID", Type.GetType("System.String"));
                dt.Columns.Add("UserName", typeof(String));  //等效于dt.Columns.Add("UserName", Type.GetType("System.String"));
                dt.Columns.Add("Age", typeof(int)); //等效于dt.Columns.Add("Age", Type.GetType("System.Int32"));
    
                //DataTable添加行数据
                dr = dt.NewRow();
                dr["UserID"] = "001";
                dr["UserName"] = "Lucky";
                dr["Age"] = 12;
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["UserID"] = "002";
                dr["UserName"] = "Zack";
                dr["Age"] = 13;
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["UserID"] = "003";
                dr["UserName"] = "Poppy";
                dr["Age"] = 15;
                dt.Rows.Add(dr);
    
                //查询年龄大于13的用户
                //1. 查询结果转成DataTable
                Console.WriteLine("查询结果转成DataTable");
                DataTable tempDataTable = (from p in dt.AsEnumerable()
                                           where p.Field<int>("Age") > 12
                                           select p).CopyToDataTable<DataRow>();
                for (int i = 0; i < tempDataTable.Rows.Count; i++)
                {
                    for (int j = 0; j < tempDataTable.Columns.Count; j++)
                    {
                        Console.Write(tempDataTable.Rows[i][j].ToString() + "	");
                    }
                }
                Console.WriteLine();
    
                //2. 查询结果转成匿名类(推荐)
                Console.WriteLine("查询结果转成匿名类");
                var query = from p in dt.AsEnumerable()
                            where p.Field<int>("Age") > 12
                            select new { UserID = p.Field<string>("UserID"), UserName = p.Field<string>("UserName"), Age = p.Field<int>("Age") };
                foreach (var item in query)
                {
                    Console.Write(item.UserID + "	" + item.UserName + "	" + item.Age + "	");
                }
                Console.ReadLine();
            }
        }
    }
    View Code

    二、ToArray操作符

    ToArray操作符可以在IEnumerable<T>类型的任何派生对象上调用,返回值为T类型的数组

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

    三、ToDictionary操作符

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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace test
    {
        public class Category
        {
            public int Id { get; set; }
            public string CategoryName { get; set; }
            public DateTime CreateTime { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                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);//注意:Key不能有相同的
                foreach (var item in dict)
                {
                    Console.WriteLine("key:{0},value:{1}", item.Key, item.Value);
                }
                Console.ReadLine();
            }
        }
    }
    View Code

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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace test
    {
        public class Category
        {
            public int Id { get; set; }
            public string CategoryName { get; set; }
            public DateTime CreateTime { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                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);
                foreach (var item in dict)
                {
                    Console.WriteLine("key:{0},Id:{1},CategoryName:{2},CreateTime:{3}",
                        item.Key, dict[item.Key].Id, dict[item.Key].CategoryName, dict[item.Key].CreateTime);
                }
                Console.ReadLine();
            }
        }
    }
    View Code

     

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

    四、ToList操作符

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

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

    五、ToLookUp操作符

    ToLookUp操作符将创建一个LookUp<TKey,TElement>对象,这是一个one-to-many的集合,一个key可以对应多个value值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace test
    {
        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; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                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:{0}", item.Key);
                    foreach (var p in item)
                    {
                        Console.WriteLine("value:{0}", p);
                    }
                }
                Console.ReadLine();
            }
        }
    }
    View Code

    注意:

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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace test
    {
        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; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                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);//有点类似于分组
                foreach (var item in list)
                {
                    Console.WriteLine("key:{0}", item.Key);
                    foreach (var p in item)
                    {
                        Console.WriteLine("Id:{0},CategoryId:{1},Name:{2},CreateTime:{3}", p.Id, p.CategoryId, p.Name, p.CreateTime);
                    }
                }
                Console.ReadLine();
            }
        }
    }
    View Code

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

    六、Cast操作符

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

    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。(也就是说集合里面必须是同类型的,例如不能出现字符串和数字元素)

  • 相关阅读:
    POJ1785 Binary Search Heap Construction
    Bzoj1185 [HNOI2007]最小矩形覆盖
    POJ2409 Let it Bead
    Bzoj2732 [HNOI2012]射箭
    Bzoj4515 [Sdoi2016]游戏
    Bzoj3925 [Zjoi2015]地震后的幻想乡
    Bzoj3223 Tyvj 1729 文艺平衡树
    COGS2642 / Bzoj4590 [Shoi2015]自动刷题机
    Bzoj1313 [HAOI2008]下落的圆盘
    python——描述符
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12742580.html
Copyright © 2011-2022 走看看