<%@ 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 = ;
var v2 = 12.5; //v = "abc";//错
int i;
i = ;
//var v3;//错
//v3 = 20;
}
//自动属性
protected void Button2_Click(object sender, EventArgs e)
{
Student stu = new Student();
stu.Sex = "男";
stu.StuName = "张三";
stu.StuId = ""; }
//对象初始化
protected void Button3_Click(object sender, EventArgs e)
{
//可以少写构造函数,
Student stu = new Student() { StuId="" };
Student stu1 = new Student() { StuName = "李四", StuId = "" };
Student stu2 = new Student() { Sex = "男" };
}
//集合初始化器
protected void Button4_Click(object sender, EventArgs e)
{
int[] arr = { , , , };
List<int> list = new List<int>() { , , , }; Response.Write("长度:"+list.Count);
//例定义一个list,默认存放三个学生
List<Student> list1 = new List<Student>() {
new Student(){ StuId="",StuName="李四",Sex="男"},
new Student(){ StuId="",StuName="张三",Sex="女"},
new Student(){ StuId="", StuName="王五",Sex="男"} }; }
//匿名类型
protected void Button5_Click(object sender, EventArgs e)
{
//直接创建分数对象
var g = new { Gid = "", Score = , StuId = "" };//编译器会为我们自动生成一个类,类中包函以上属性
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(,);
Response.Write(i); //需要用Lambda表达式的扩展方法
List<int> list=new List<int>(){ ,,,,,};
int s = list.Sum();
Response.Write("所有人的成绩:"+s);
//把及格人的总分算出来
//func<方法的参数类型,方法的返回值类型>
Func<int, bool> f = (a) => a > ;
//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实例的更多相关文章

  1. Linq:Linq实例1..More

    本文会不断更新应用实例. 需求1:对一个Rate列表的RateLevel属性求和,然后除以Rate列表的数量求平均值. 没有Linq的做法: Int rateLevel = ; foreach (Ra ...

  2. 经典Linq实例语句

    从技术角度而言,LINQ定义了大约40个查询操作符,如select.from.in.where以及order by(C#中).使用这些操作符可以编写查询语句.不过,这些查询还可以基于很多类型的数据,每 ...

  3. Linq 实例

    1.分页 ).Take(); 2.分组 1)一般分组 //根据顾客的国家分组,查询顾客数大于5的国家名和顾客数var 一般分组 = from c in ctx.Customers group c by ...

  4. linq学习三个实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. linq中let关键字学习

    linq中let关键字就是对子查询的一个别名,let子句用于在查询中添加一个新的局部变量,使其在后面的查询中可见. linq中let关键字实例 1.传统下的子查询与LET关键字的区别     C# 代 ...

  6. 转 --简单解决Linq多条件组合问题

    本文笔者用清晰的实例,解决了Linq多条件问题,思路十分的清晰,笔者也很细心的做了描述,希望能给你带来帮助. 最近有个项目准备功能改版,师兄吩咐:尽可能地做到万般皆Linq,所以很多东西都要从存储过程 ...

  7. WebApi-JSON序列化循环引用

    Overview 最近被序列化,循环引用的问题,让我浑身酸爽.遇到这种异常是在搭建WebApi的时候,当我返回Linq实例类集合的时候出现的. 下定决心要解决这个问题.循环引用引起的原因是: 比如说: ...

  8. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  9. LinQ C#防注入式攻击实例代码

    注入式攻击是Web开放项目中开发人员的第一时间要考虑的问题,下面就我的开发实例分享给大家,有用的的话就点个赞吧. 定義賬戶信息類 public class UserInfors { public st ...

随机推荐

  1. oracle 分区表的维护

    1:添加分区: ALTER TABLE SALES ADD PARTITION P3 VALUES LESS THAN(TO_DATE('2003-06-01','YYYY-MM-DD')); SAL ...

  2. 关于科台斯k97gprs调试记录(1)

    模块调试 1.gprs模块了解 用流量上网的模块,可以发短信,打电话. 2.AT指令的学习 AT+UART=波特率,流控位,数据位长度,校验控制,停止位长度 AT+NET=TCP/UDP 选择,APN ...

  3. OpenStack 服务状态检查

    openstack服务不正常 使用命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@node-5 TimaIaas]# nova- ...

  4. expandlistview

    package com.exaple.zhonghe2; import java.sql.SQLData;import java.util.ArrayList;import java.util.Has ...

  5. Objective-C代码的文件扩展名与数据类型

    Objective-C数据类型可以分为:基本数据类型.对象类型和id类型. 基本数据类型有:int.float.double和char类型. 对象类型就是类或协议所声明的指针类型,例如:SAutore ...

  6. Asp.Net MVC 模型验证详解-实现客户端、服务端双重验证

    概要 在asp.net webform开发中经常会对用户提交输入的信息进行校验,一般为了安全起见大家都会在客户端进行Javascript(利于交互).服务端双重校验(安全).书写校验代码是一个繁琐的过 ...

  7. gerrit docker运行失败 chown: /var/gerrit/review_site: Permission denied 【已解决】

    Docker Volume 之权限管理(转) - jackluo - 博客园 http://www.cnblogs.com/jackluo/p/5783116.html 为什么在公司电脑没有问题,但在 ...

  8. linux下rm命令修改,增加回收站功能【笔记】

    一个脚本,linux的用户根目录下.bashrc最后加入如下代码,可以修改rm命令,让人们rm时候不再会全部删除,而是会加入到回收站里,以下是根据别人的资料参考修改的,不是原创 加入后,需要sourc ...

  9. js 如何生成唯一且不可预测的 ID

    通常数据库可以生成唯一的 ID,最多的就是数字序列,也有像 MongoDB 这样产生组合序列的,不过这种形式的 ID 由于是序列,是可以预测的.如果想得到不可预测且唯一的 ID,方法还是有的. 下面主 ...

  10. 测试驱动开发神器框架Mockito

    作为菜鸟的我,以前没接触过Mock类型的框架,比如说要测试action层,我总是从action层调用service再调用dao访问数据库,这种方式从原则上来说是无疑是非常正确的,在没用mock框架之前 ...