Linq实例
<%@ 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实例的更多相关文章
- Linq:Linq实例1..More
本文会不断更新应用实例. 需求1:对一个Rate列表的RateLevel属性求和,然后除以Rate列表的数量求平均值. 没有Linq的做法: Int rateLevel = ; foreach (Ra ...
- 经典Linq实例语句
从技术角度而言,LINQ定义了大约40个查询操作符,如select.from.in.where以及order by(C#中).使用这些操作符可以编写查询语句.不过,这些查询还可以基于很多类型的数据,每 ...
- Linq 实例
1.分页 ).Take(); 2.分组 1)一般分组 //根据顾客的国家分组,查询顾客数大于5的国家名和顾客数var 一般分组 = from c in ctx.Customers group c by ...
- linq学习三个实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linq中let关键字学习
linq中let关键字就是对子查询的一个别名,let子句用于在查询中添加一个新的局部变量,使其在后面的查询中可见. linq中let关键字实例 1.传统下的子查询与LET关键字的区别 C# 代 ...
- 转 --简单解决Linq多条件组合问题
本文笔者用清晰的实例,解决了Linq多条件问题,思路十分的清晰,笔者也很细心的做了描述,希望能给你带来帮助. 最近有个项目准备功能改版,师兄吩咐:尽可能地做到万般皆Linq,所以很多东西都要从存储过程 ...
- WebApi-JSON序列化循环引用
Overview 最近被序列化,循环引用的问题,让我浑身酸爽.遇到这种异常是在搭建WebApi的时候,当我返回Linq实例类集合的时候出现的. 下定决心要解决这个问题.循环引用引起的原因是: 比如说: ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- LinQ C#防注入式攻击实例代码
注入式攻击是Web开放项目中开发人员的第一时间要考虑的问题,下面就我的开发实例分享给大家,有用的的话就点个赞吧. 定義賬戶信息類 public class UserInfors { public st ...
随机推荐
- HttpUtility.HtmlEncode 方法
將字串轉換為 HTML 編碼的字串. 例如: publicstringWelcome(string name,int numTimes =1){ returnHttpUtility.HtmlE ...
- Visual Studio 2012 怪异的自动重启
学生在做项目的过程中遇到这种问题: -------------- 用 Visual Studio 2012 开发W中eb 项目时,最近总是莫名其妙的自动重启. 后来试了一下,发现是只要在页面中输入 ...
- 【py分析】
pyQuery pyQuery 是 jQuery 在 python 中的实现,能够以 jQuery 的语法来操作解析 HTML 文档,十分方便.使用前需要安装,easy_install pyquery ...
- 由 "select *" 引发的“惨案”
今天凌晨做发布, 要合并多个分数据库的表数据到主数据库中, 有 30+ 分数据库. 前面都比较顺利, 在临近结束时,突然发现一个字段的值插入错误. 有一个表 T,字段分别为 (f1, f2, f3, ...
- Linux 的 Crontab 命令运用(转)
cron来源于希腊单词chronos(意为“时间”),是linux系统下一个自动执行指定任务的程序.例如,你想在每晚睡觉期间创建某些文件或文件夹的备份,就可以用cron来自动执行. 服务的启动和停止 ...
- webpack笔记_(1)_webpack 安装
webpack不仅可以解析jsx,也可以将es6转换为es5语法.最终,它把这些代码都打包成一个叫bundle.js的文件,我们在html文件中只引入这么一个js文件就可以了! 打包后,引用的语法im ...
- laravel队列
三种情况: queue:work 默认只执行一次队列请求, 当请求执行完成后就终止; queue:listen 监听队列请求, 只要运行着, 就能一直接受请求, 除非手动终止; queue:work ...
- peak num
class Solution {public: int findPeakElement(vector<int>& nums) { int i=0; int n=nums ...
- [转]How Can I Find Out What Is Using a Busy or Reserved Serial Port?
转自:http://digital.ni.com/public.nsf/allkb/29B079481C5ECE76862578810082394E How Can I Find Out What I ...
- ubunt1204安装配置vsftp
本文将搭建一个最简单的ftp服务,即通过root用户可进行登录.上传.下载,具体步骤如下: 1.安装vsftpd服务 sudo apt-get install vsftpd 2.编辑vsftp配置文件 ...