首页 > 其他 > 详细

Linq实例

时间:2016-03-04 19:16:37      阅读:175      评论:0      收藏:0      [点我收藏+]

技术分享

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication1.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" Text="隐式类型局部变量" onclick="Button1_Click" />
    
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="自动属性" />
    
        <br />
        <asp:Button ID="Button3" runat="server" Text="对象初始化器" onclick="Button3_Click" />
    
        <br />
        <asp:Button ID="Button4" runat="server" Text="集合初始化器" onclick="Button4_Click" />
    
        <br />
        <asp:Button ID="Button5" runat="server" Text="匿名类型" onclick="Button5_Click" />
    
        <br />
        <asp:Button ID="Button6" runat="server" Text="扩展方法" onclick="Button6_Click" 
            style="height: 21px" />
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button7" runat="server" Text="Lambda表达式" 
            onclick="Button7_Click" />
    
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    //委托类型
    public delegate int DelDemo(int a,int b);

    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //隐式类别局部变量
            //1.定义时必须给值
            //2.定义时变量的类型会根据值自动识别,所以在重新赋值时给的值必须对应类型的值
            //3.局部变量可以用它定义
            var v = 10;
            var v2 = 12.5;

            //v = "abc";//
            int i;
            i = 10;
            //var v3;////v3 = 20;
        }
        //自动属性
        protected void Button2_Click(object sender, EventArgs e)
        {
            Student stu = new Student();
            stu.Sex = "";
            stu.StuName = "张三";
            stu.StuId = "12";
                
        }
        //对象初始化
        protected void Button3_Click(object sender, EventArgs e)
        {
            //可以少写构造函数,
            Student stu = new Student() { StuId="12" };
            Student stu1 = new Student() { StuName = "李四", StuId = "13" };
            Student stu2 = new Student() { Sex = "" };
        }
        //集合初始化器
        protected void Button4_Click(object sender, EventArgs e)
        {
            int[] arr = { 10, 20, 30, 40 };
            List<int> list = new List<int>() { 102, 221, 411, 225 };

            Response.Write("长度:"+list.Count);
            //例定义一个list,默认存放三个学生
            List<Student> list1 = new List<Student>() {
            new Student(){ StuId="1001",StuName="李四",Sex=""},
            new Student(){ StuId="1002",StuName="张三",Sex=""},
            new Student(){ StuId="1003", StuName="王五",Sex=""} };
           


        }
        //匿名类型
        protected void Button5_Click(object sender, EventArgs e)
        {
            //直接创建分数对象
            var g = new { Gid = "1001", Score = 89, StuId = "1001" };//编译器会为我们自动生成一个类,类中包函以上属性
            Response.Write("分数:"+g.Score);
        }
        //扩展方法
        protected void Button6_Click(object sender, EventArgs e)
        {
            string pwd = TextBox1.Text;
            string str = pwd.MD5();
            string str1 = pwd.SHA1();
            Response.Write("MD5加密后"+str);
            Response.Write("SHA1加密后" + str1);
            this.Alert("你好");
        }
        public int Sum(int a, int b)
        {
            return a + b;
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            //Lambda表达本质上就是一个委托对象
            DelDemo dd = new DelDemo(Sum);//委托的标准写法
            //2.C#2.0中的匿名方法的写法
            DelDemo dd2 = delegate(int a, int b)
            {
                return a - b;
            };
            //3.C#3.0中退出的Lambda表达式的写法
            DelDemo dd3 = (a, b) => a - b;//{int x=a-b};

            //调用委托的方法
            int i = dd3(10,20);
            Response.Write(i);

             //需要用Lambda表达式的扩展方法
        List<int> list=new List<int>(){ 54,20,67,44,77,89};
        int s = list.Sum();
        Response.Write("所有人的成绩:"+s);
            //把及格人的总分算出来
            //func<方法的参数类型,方法的返回值类型>
        Func<int, bool> f = (a) => a > 60;
            //where 查询符合委托条件的数,以集合返回
            //IEnumerable是所有数组与集合父接口
        IEnumerable<int> ds = list.Where(f);
        int d = ds.Sum();
        Response.Write("结果:"+d);
            
        
        }

       

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
   
    public class Student
    {
        string stuId;

        public string StuId
        {
            get { return stuId; }
            set { stuId = value; }
        }

        string stuName;

        public string StuName
        {
            get { return stuName; }
            set { stuName = value; }
        }
        //1.自动属性,在编译器编译时,会为这个属性生成一个变量
        //get与set操作的是这个变量
        //2.get与set必须都有

        public string Sex{ get;set; }
       
    }
}

 

Linq实例

原文:http://www.cnblogs.com/xiaz/p/5243086.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!