• DotNET:LINQ对泛型(Generic)操作和泛型方法及属性


    ylbtech-DotNET:泛型(Generic)和LINQ操作
    技术与环境 返回顶部
    操作系统: windows 语言类别: C#
    thankyou: ylbtech, 谢谢你的默默付出 课题: 泛型(Generic)和LINQ操作
    学习软件: Microsoft Visaul Studio 2010
    1,什么是泛型和LINQ?
     
    2,LINQ对泛型(Generic)操作和泛型方法及属性代码 返回顶部
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    using System.Collections;
    public partial class Geniric : System.Web.UI.Page
    {
        /// <summary>
        /// 1,遍历泛型集合
        /// </summary>
        /// <param name="dals"></param>
        private void Write(IList<PersonInfo> dals)
        {
            string tableHead = "<table style='background-color:yellow;'><tr><td>编号</td><td>姓名</td><td>年龄</td><td>性别</td></tr>";
            string tableTr = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>";
            string tableFoot = "</table>";
            Response.Write(tableHead);
            foreach (PersonInfo dal in dals)
            {
                Response.Write(string.Format(tableTr, dal.Id, dal.Name, dal.Age, dal.Sex));
            }
            Response.Write(tableFoot);
        }
        /// <summary>
        /// 2,根据索引位置移除项
        /// </summary>
        /// <param name="dals"></param>
        /// <param name="index"></param>
        private void RemoveAt(IList<PersonInfo> dals, int index)
        {
            dals.RemoveAt(index);
        }
        /// <summary>
        /// 3,查询一个人
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="dals"></param>
        private void Single(int id, IList<PersonInfo> dals)
        {
    
            PersonInfo dal = dals.Single(c => c.Id == 2);
    
            if (dal != null)
            {
                Response.Write(string.Format("编号:{0},姓名:{1},年龄:{2},性别:{3}。", dal.Id, dal.Name, dal.Age, dal.Sex));
            }
            else
            {
                Response.Write("此编号不存在。");
            }
        }
        /// <summary>
        /// 3.2,查询一个人(复合条件查询(同时满足))
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="name">姓名</param>
        /// <param name="dals"></param>
        private void Single(int id, string name, IList<PersonInfo> dals)
        {
    
            PersonInfo dal = dals.Single(c => c.Id == 2 && c.Name == name);
    
            if (dal != null)
            {
                Response.Write(string.Format("编号:{0},姓名:{1},年龄:{2},性别:{3}。", dal.Id, dal.Name, dal.Age, dal.Sex));
            }
            else
            {
                Response.Write("此编号不存在。");
            }
        }
        /// <summary>
        /// 3.3,查询一个人(复合条件查询(任何一个满足即可))
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="name">姓名</param>
        /// <param name="dals"></param>
        private void Single2(int id, string name, IList<PersonInfo> dals)
        {
    
            PersonInfo dal = dals.Single(c => c.Id == 2 || c.Name == name);
    
            if (dal != null)
            {
                Response.Write(string.Format("编号:{0},姓名:{1},年龄:{2},性别:{3}。", dal.Id, dal.Name, dal.Age, dal.Sex));
            }
            else
            {
                Response.Write("此编号不存在。");
            }
        }
        /// <summary>
        /// 4,属性列表
        /// </summary>
        private void Attributes(IList<PersonInfo> dals)
        {
            Write(dals);
            Response.Write("<hr>");
    
            //attr:1,
            Response.Write(string.Format("包含项数量:{0}<br>", dals.Count));
    
            var idSum = dals.Sum(p => p.Id);
            Response.Write(string.Format("编号总和:{0}<br>", idSum));
    
            var ageAvg = dals.Average(p => p.Age);
            Response.Write(string.Format("平均年龄:{0}<br>", ageAvg));
    
            var ageMax = dals.Max(p => p.Age);
            Response.Write(string.Format("最大年龄:{0}<br>", ageMax));
    
            var ageMin = dals.Min(p => p.Age);
            Response.Write(string.Format("最小年龄:{0}<br>", ageMin));
    
    
        }
        /// <summary>
        /// 5,把泛型转为PersonInfo数组集合,并遍历输出
        /// </summary>
        /// <param name="dals"></param>
        private void ToArray(IList<PersonInfo> dals)
        {
            PersonInfo[] pArray = dals.ToArray();
    
            foreach (PersonInfo p in pArray)
            {
                Response.Write(string.Format("编号:{0},姓名:{1},年龄,{2}性别:{3}<br>", p.Id, p.Name, p.Age, p.Sex));
            }
        }
        /// <summary>
        /// 6,把泛型转为Dictionary数据词典,并遍历输出
        /// </summary>
        /// <param name="dals"></param>
        private void ToDictionary(IList<PersonInfo> dals)
        {
            Dictionary<int, PersonInfo> dalsDict = dals.ToDictionary(p => p.Id);
    
            foreach (int key in dalsDict.Keys)
            {
                PersonInfo dalDict = dalsDict[key];
                Response.Write(string.Format("编号:{0},姓名:{1},年龄,{2}性别:{3}<br>", dalDict.Id, dalDict.Name, dalDict.Age, dalDict.Sex));
            }
        }
        /// <summary>
        /// 7,把泛型转为List列表,并遍历输出
        /// </summary>
        /// <param name="dals"></param>
        private void ToList(IList<PersonInfo> dals)
        {
            List<PersonInfo> dalsList = dals.ToList();
    
            foreach (PersonInfo p in dalsList)
            {
                Response.Write(string.Format("编号:{0},姓名:{1},年龄,{2}性别:{3}<br>", p.Id, p.Name, p.Age, p.Sex));
            }
        }
        /// <summary>
        /// 8,把泛型转为ILookup,
        /// 【没做出】
        /// </summary>
        /// <param name="dals"></param>
        private void ToLookup(IList<PersonInfo> dals)
        {
            ILookup<int, string> dalsLookup = dals.ToLookup(p => p.Id, p => p.Name);
    
            for (int i = 0; i < dalsLookup.Count; i++)
            {
                //Response.Write(string.Format("{0}<br>",dalsLookup[i].));
            }
        }
        /// <summary>
        /// 9,
        /// 【没做出】
        /// </summary>
        /// <param name="dals"></param>
        private void UnionWhereZip(IList<PersonInfo> dals)
        {
            IList<PersonInfo> dals2 = new List<PersonInfo>();
            dals2.Add(new PersonInfo() { Id = 11, Name = "rain", Age = 23, Sex = "" });
            dals2.Add(new PersonInfo() { Id = 22, Name = "tly", Age = 30, Sex = "" });
            dals2.Add(new PersonInfo() { Id = 3, Name = "zyn", Age = 25, Sex = "" });
            dals2.Add(new PersonInfo() { Id = 4, Name = "yxq", Age = 23, Sex = "" });
            dals2.Add(new PersonInfo() { Id = 5, Name = "yhw", Age = 26, Sex = "" });
    
    
            dals.Union(dals2);
            Write(dals);
        }
        /// <summary>
        /// 10,排序
        /// 【没做出】
        /// </summary>
        /// <param name="dals"></param>
        private void OrderBy(IList<PersonInfo> dals)
        {
            Write(dals);
            Response.Write("<hr>");
            dals.OrderBy(p => p.Age);
    
            Write(dals);
        }
        /// <summary>
        /// ylbtech:对泛型集合操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<PersonInfo> dals = new List<PersonInfo>();
            dals.Add(new PersonInfo() { Id = 1, Name = "rain", Age = 23, Sex = "" });
            dals.Add(new PersonInfo() { Id = 2, Name = "tly", Age = 30, Sex = "" });
            dals.Add(new PersonInfo() { Id = 3, Name = "zyn", Age = 25, Sex = "" });
            dals.Add(new PersonInfo() { Id = 4, Name = "yxq", Age = 23, Sex = "" });
            dals.Add(new PersonInfo() { Id = 5, Name = "yhw", Age = 26, Sex = "" });
    
            //输出年龄最大
            //1,
            //Write(dals);
    
            //2,
            //RemoveAt(dals,1);
            //Write(dals);
    
            //3,
            //Single(2, dals);
    
            //dals.Clear();   //移除所有的项
            //Write(dals);
    
            //4,
            //Attributes(dals);
    
            //5,
            //ToArray(dals);       
    
            //6,
            //ToDictionary(dals);
    
            //7,
            //ToList(dals);
    
            //8,
            //ToLookup(dals);
    
    
            // IEnumerable<PersonInfo> dalIEnum = dals.Where(p => p.Id == 1);
    
            //PersonInfo dal = (PersonInfo)dals.Where(p => p.Id == 1);
    
            //dalIEnum.
    
    
            Response.End();
        }
    }
    3,使用泛型(Generic)有哪些好处? 返回顶部
        我们现在对泛型做一个总结:
    • 泛型类和泛型方法同时具备可重用性、类型安全和效率,这是非泛型类和非泛型方法无法具备的。
    • 泛型是c#2.0的一个新增加的特性,它为使用c#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    OAuth2.0介绍
    C#集成FastDFS断点续传
    ABP集成WIF实现单点登录
    CentOS7下搭建FastDfs(V5.11)+Keepalived分布式集群部署
    【算法】之常见的排序算法
    【开发工具
    【C/C++】之C/C++快速入门
    【数据结构】之二叉树(Java语言描述)
    【数据结构】之散列链表(Java语言描述)
    【数据结构】之队列(Java语言描述)
  • 原文地址:https://www.cnblogs.com/ylbtech/p/2860104.html
Copyright © 2020-2023  润新知