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

简介:
ylbtech-DotNET:泛型(Generic)和LINQ操作
技术与环境返回顶部
操作系统: windows 语言类别: C#
thankyou: ylbtech, 谢谢你的默默付出 课题: 泛型(Generic)和LINQ操作
学习软件: Microsoft Visaul Studio 2010
1,什么是泛型和LINQ?

引用: 百度百科-泛型 
引用: 百度百科-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#语言编写面向对象程序增加了极大的效力和灵活性。不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,所以性能得到提高。
本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2013/01/15/2860104.html ,如需转载请自行联系原作者
相关文章
|
3月前
|
SQL 开发框架 .NET
聊聊 System.Linq.Dynamic,以及分享一个使用 System.Linq.Dynamic 扩展 LINQ 查询的详细例子
聊聊 System.Linq.Dynamic,以及分享一个使用 System.Linq.Dynamic 扩展 LINQ 查询的详细例子
110 0
|
前端开发 Java API
Java反射机制(Reflection)简解与示例
Java反射机制(Reflection)简解与示例
89 0
|
安全 Java 容器
Java泛型(1)--集合使用泛型Generic、自定义泛型、泛型在继承上的体现、通配符的使用
Java泛型(1)--集合使用泛型Generic、自定义泛型、泛型在继承上的体现、通配符的使用
169 0
Java泛型(1)--集合使用泛型Generic、自定义泛型、泛型在继承上的体现、通配符的使用
EntityFramework——委托与Lambda表达式
EntityFramework——委托与Lambda表达式
132 0
EntityFramework——委托与Lambda表达式
|
开发框架 自然语言处理 .NET
Linq To Object——三种委托
Linq To Object——三种委托
130 0
|
存储 SQL 开发框架
Linq To Object标准操作符最强总结
Linq To Object标准操作符最强总结
140 0
|
安全
泛型Generic(1.概述)
泛型类和泛型方法兼复用性、类型安全和高效率于一身,是与之对应的非泛型类和方法所不及。
1043 0
|
.NET 数据库 Windows
|
.NET 开发框架 编译器