zoukankan      html  css  js  c++  java
  • LINQ 学习路程 -- 查询操作 Conversion Operators

    MethodDescription
    AsEnumerable Returns the input sequence as IEnumerable<t>
    AsQueryable Converts IEnumerable to IQueryable, to simulate a remote query provider
    Cast Coverts a non-generic collection to a generic collection (IEnumerable to IEnumerable<T>)
    OfType Filters a collection based on a specified type
    ToArray Converts a collection to an array
    ToDictionary Puts elements into a Dictionary based on key selector function
    ToList Converts collection to List
    ToLookup Groups elements into an Lookup<TKey,TElement>
    class Program
    {
    
        static void ReportTypeProperties<T>(T obj)
        {
            Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
            Console.WriteLine("Actual type: {0}", obj.GetType().Name);
        }
    
        static void Main(string[] args)
        {
            Student[] studentArray = { 
                    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                    new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
                    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
                    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
                    new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
                };   
                
            ReportTypeProperties( studentArray);
            ReportTypeProperties(studentArray.AsEnumerable());
            ReportTypeProperties(studentArray.AsQueryable());   
        }
    }    




    Cast
    class Program
    {
    
        static void ReportTypeProperties<T>(T obj)
        {
            Console.WriteLine("Compile-time type: {0}", typeof(T).Name);
            Console.WriteLine("Actual type: {0}", obj.GetType().Name);
        }
    
        static void Main(string[] args)
        {
            Student[] studentArray = { 
                    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
                    new Student() { StudentID = 2, StudentName = "Steve",  Age = 21 } ,
                    new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
                    new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
                    new Student() { StudentID = 5, StudentName = "Ron" , Age = 31 } ,
                };   
             
            ReportTypeProperties( studentArray);
            ReportTypeProperties(studentArray.Cast<Student>());
        }
    }      
     
    IList<string> strList = new List<string>() { 
                                                "One", 
                                                "Two", 
                                                "Three", 
                                                "Four", 
                                                "Three" 
                                                };
    
    string[] strArray = strList.ToArray<string>();// converts List to Array
    
    IList<string> list = strArray.ToList<string>(); // converts array into list










    IList<Student> studentList = new List<Student>() { 
                        new Student() { StudentID = 1, StudentName = "John", age = 18 } ,
                        new Student() { StudentID = 2, StudentName = "Steve",  age = 21 } ,
                        new Student() { StudentID = 3, StudentName = "Bill",  age = 18 } ,
                        new Student() { StudentID = 4, StudentName = "Ram" , age = 20 } ,
                        new Student() { StudentID = 5, StudentName = "Ron" , age = 21 } 
                    };
    
    //following converts list into dictionary where StudentId is a key
    IDictionary<int, Student> studentDict = 
                                    studentList.ToDictionary<Student, int>(s => s.StudentID); 
    
    foreach(var key in studentDict.Keys)
    	Console.WriteLine("Key: {0}, Value: {1}", 
                                    key, (studentDict[key] as Student).StudentName);
    
    
  • 相关阅读:
    Ansible 详细用法说明(一)
    Puppet基于Master/Agent模式实现LNMP平台部署
    推荐-zabbix原理篇
    Centos 6.x 安装Nagios及WEB管理nagiosql实现windows及linux监控指南
    CentOS 7下安装Logstash ELK Stack 日志管理系统(下)
    【Python基础学习二】定义变量、判断、循环、函数基本语法
    【Python基础学习一】在OSX系统下搭建Python语言集成开发环境 附激活码
    内联函数
    2016 科大讯飞校招研发一面二面 10.13
    hiho #1151 : 骨牌覆盖问题·二 (递推,数论)
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6608118.html
Copyright © 2011-2022 走看看