<%@ 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. paper 69:Haar-like矩形遍历检测窗口演示Matlab源代码[转载]

    Haar-like矩形遍历检测窗口演示Matlab源代码 clc; clear; close all; % Haar-like特征矩形计算 board = 24 % 检测窗口宽度 num = 24 % ...

  2. rails创建项目,部署,测试流程(rails5.0+ruby2.3.1)

    rails new test_app --skip-test-unit 不生成默认的test,稍后用rspeccd test_app 修改Gemfile(大部分为自动生成) source 'https ...

  3. Linux(CentOS) 如何查看当前占用CPU或内存最多的K个进程

    一.可以使用以下命令查使用内存最多的K个进程 方法1: ps -aux | sort -k4nr | head -K 如果是10个进程,K=10,如果是最高的三个,K=3 说明:ps -aux中(a指 ...

  4. 深入了解webservice_概念总结

    最近公司需要对java web端的第三方接口进行测试,使用WebService+TestNG实现,TsetNg是常用的自动化测试框架,这就不介绍了. WebService是一种跨编程语言和跨操作系统平 ...

  5. [sinatra] Just Do It: Learn Sinatra, Part One Darren Jones

    1. Install sinatra gem gem install sinatra --no-ri --no-rdoc 2. Basic App #!/usr/bin/ruby require 's ...

  6. php ssh2 scp问题解决

    <?php$connection = ssh2_connect('192.168.1.XX', 22);$res=ssh2_auth_password($connection, 'root', ...

  7. makefile 中 $@ $^ %< 使用【转】

    转自:http://blog.csdn.net/kesaihao862/article/details/7332528 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将 ...

  8. 装多系统删除某个系统后,如何恢复ubuntu引导

    在重装系统或者再装多个系统后可能会出现ubuntu的引导文件不存在的情况,windows系列的引导文件可以用winpe修复,但是ubuntu就不可以,虽然网上很多种修复ubuntu的引导文件 方式,但 ...

  9. MySQL Replication浅析

    MySQL Replication是MySQL非常出色的一个功能,该功能将一个MySQL实例中的数据复制到另一个MySQL实例中.整个过程是异步进行的,但由于其高效的性能设计,复制的延时非常小.MyS ...

  10. phpize 动态添加 PHP 扩展的错误及解决方案

    使用phpize 动态添加 PHP 扩展是开发中经常需要做的事情,但是在 macOS 中,首次使用该功能必然会碰到一些错误,本文列出了这些错误的解决方法. 问题一: 执行 phpize 报错如下: $ ...