Lambda,MVC 练习(20101117)

   复习以前学过的东西 发现大部分东西都忘记了。悲剧。后来总结发现应该记个记号,以后没事的时候翻出来瞅两眼,忘的就不是那么快了。那都是必须的。

   摘至MSDN :Lambda

 

1、“Lambda 表达式”是一个并匿名函数,它可以包含表达式和语句,且可用于创建委托或表达式树类型。

所有 Lambda 表达式都使用 Lambda 运算符=> 该运算符读为“goes to”。 该 Lambda 运算符的左边是输入参数(如果有),右边包含表达式或语句块。 Lambda 表达式 x => x * x 读作“x goes to x times x”。可以将此表达式分配给委托类型.

 

2、=> 运算符具有与赋值运算符 (=) 相同的优先级,并且是右结合运算符。

lambda=>Msdn.lambdaInfo.

 

  1、建立一个MVC工程.

 2、建立以个文件夹并命名为components与Views文件夹同级.

    其次在components下建立类Mycompares其代码如下:

Mycompare.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace LambdaMvcPractice.Components
{
    
public class Mycompare : IComparer<int>
    {
        
public int Compare(int x, int y)
        {
            
int x1 = Math.Abs(x);
            
int y1 = Math.Abs(y);
            
if (x1 > y1)
            {
                
return 1;
            }
            
else if (x1 == y1)
            {
                
return 0;
            }
            
else
            {
                
return -1;
            }
        }
    }
}


 3、LambdaController.cs

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

namespace LambdaMvcPractice.Controllers
{
    
public class LambdaController : Controller
    {
        
//
        
// GET: /Lambda/

        
int[] Age = { 12345678910 };
        
//1、Where()
        public ActionResult Where()
        {
            
//声明一数组
            
//通过Where来过滤 能被2整除的数
            ViewData["NewAgeList"= Age.Where(n => n % 2 == 0);
            ViewData[
"NewAgeListByIndex"= Age.Where((num, index) => num >= index * 2);
            
return View();
        }

        
//2、OrderBy()
        public ActionResult OrderBy()
        {

            
int[] C = { 1-23-45-67-89-10 };
            
//除10后升序排列
            ViewData["OrderByAsc"= Age.OrderBy(M => M % 10);
            
//除10后降序排列
            ViewData["OrderByDes"= Age.OrderByDescending(M => M % 10);
            
//通过Mycompare类中的compare方法来进行升序排列
            ViewData["CompareList"= C.OrderBy(M => M, new Components.Mycompare());
            
return View();
        }

        
//3、Skip
        public ActionResult Skip()
        {
            
//int[] Age = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            
//跳过前3个元素
            ViewData["SkipList"= Age.Skip(3);
            
//跳出小于7的元素
            ViewData["SkipWhile"= Age.SkipWhile(M => M < 7);
            
return View();
        }
        
//4、Take
        public ActionResult Take()
        {
            
int[] A = { 12345678910 };
            
//提取前9个元素,在跳出前小于4的元素,在取出小于7的元素.(如果这里lambda表达式为:M=>M>7,显示不出数据(越界))
            ViewData["TakeList"= A.Take(9).SkipWhile(M => M < 4).TakeWhile(M => M < 7);
            
//提取小于5的元素跳出前3个元素..
            ViewData["TakeWhile"= A.TakeWhile(M => M <= 5).Skip(3);
            
//不支持自动排序,不支持M>2&&M<5?,不支持M==5?.
            return View();
        }

        
//5、Min Max Avg Sum
        public ActionResult Func()
        {
            IList
<string> Fuclist = new List<string>();
            
//最小值
            Fuclist.Add(Age.Min().ToString());
            
//最大值
            Fuclist.Add(Age.Max().ToString());
            
//平均值
            Fuclist.Add(Age.Average().ToString());
            
//求和
            Fuclist.Add(Age.Sum().ToString());
            ViewData[
"Fuc"= Fuclist;

            
int[] intary = { 11223345 };
            
//过滤相同字符串
            ViewData["Distinct"= intary.Distinct();
            
return View();
        }

        
public ActionResult Concat()
        {
            
string[] strAry1 = { "Kevin""James""Ben" };
            
string[] strAry2 = { "Ken""James""cc" };
            
//左连右
            ViewData["Concat1"= strAry1.Concat(strAry2);
            
//右连左
            ViewData["Concat2"= strAry2.Concat(strAry1);
            
//并集
            ViewData["Union"= strAry1.Union(strAry2);
            
//交集
            ViewData["Intersect"= strAry1.Intersect(strAry2);
            
//差集
            ViewData["Except"= strAry1.Except(strAry2);
            
return View();
        }


    }
}


 前台Views只贴几个页面。觉得写的不是很好。如果有朋友些的好的可以指出,我好改正。

Where:

<%@ Page Title="" Language="C#" Debug="true" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits
="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2>
        Where()
<br />
        数组Arrylist={1,2,3,4,5,6,7,8,9,10}
<br />
        --------------Begin--------------
<br />
        被2整除的数
<br />
        结果:
        
<% IList<int> Arrylist = (ViewData["NewAgeList"as IEnumerable<int>).ToList<int>(); %>
        
<%= Html.Display("<br/>"%>
        
<% for (int i = 0; i < Arrylist.Count; i++%>
        
<% { %>
        
<%= Html.Label(Arrylist[i].ToString()) %>
        
<% } %>
        
<br />
        ------------------------------
<br />
        本身大于2倍索引
<br />
        结果:
        
<% IList<int> Arrylist2 = (ViewData["NewAgeListByIndex"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int j = 0; j < Arrylist2.Count; j++%>
        
<% { %>
        
<%= Html.Label(Arrylist2[j].ToString())%>
        
<% } %><br />
        ---------------End---------------
    
</h2>
</asp:Content>

 

 

 

 

Concat

Concat.aspx
<%@ Page Title="" Language="C#" Debug="true" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits
="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Concat
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2>
        Concat()
<br />
        数组strAry1 = { "Kevin", "James", "Ben" }
<br />
        strAry2 = { "Ken", "James", "cc" }
<br />
        --------------Begin--------------
<br />
        strAry1连strAry2
<br />
        结果:
        
<% IList<string> Arrylist = (ViewData["Concat1"as IEnumerable<string>).ToList<string>(); %>
        
<% for (int i = 0; i < Arrylist.Count; i++%>
        
<% { %>
        
<%= Html.Label(Arrylist[i].ToString()) %>
        
<% } %>
        
<br />
        ------------------------------
<br />
        strAry2连strAry1
<br />
        结果:
        
<% IList<string> Arrylist2 = (ViewData["Concat2"as IEnumerable<string>).ToList<string>(); %>
        
<% for (int j = 0; j < Arrylist2.Count; j++%>
        
<% { %>
        
<%= Html.Label(Arrylist2[j].ToString())%>
        
<% } %><br />
        ------------------------------
<br />
        并集
<br />
        结果:
        
<% IList<string> Arrylist3 = (ViewData["Union"as IEnumerable<string>).ToList<string>(); %>
        
<% for (int k = 0; k < Arrylist3.Count; k++%>
        
<% { %>
        
<%= Html.Label(Arrylist3[k].ToString())%>
        
<% } %><br />
        ------------------------------
<br />
        交集
<br />
        结果:
        
<% IList<string> Arrylist4 = (ViewData["Intersect"as IEnumerable<string>).ToList<string>(); %>
        
<% for (int l = 0; l < Arrylist4.Count; l++%>
        
<% { %>
        
<%= Html.Label(Arrylist4[l].ToString())%>
        
<% } %><br />
        ------------------------------
<br />
        差集
<br />
        结果:
        
<% IList<string> Arrylist5 = (ViewData["Except"as IEnumerable<string>).ToList<string>(); %>
        
<% for (int m = 0; m < Arrylist5.Count; m++%>
        
<% { %>
        
<%= Html.Label(Arrylist5[m].ToString())%>
        
<% } %><br />
        ---------------End---------------
</h2>
</asp:Content>


 

OrderBy

 

OrderBy.aspx
<%@ Page Title="" Language="C#" Debug="true" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits
="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    OrderBy
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2>
        Order()
<br />
        数组:Arrylist={1,2,3,4,5,6,7,8,9,10}
        
<br />
        ------------Begin------------
<br />
        升序
<br />
        结果:
<% IList<int> Arrlist = (ViewData["OrderByAsc"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int i = 0; i < Arrlist.Count; i++%>
        
<% { %>
        
<%= Html.Label(Arrlist[i].ToString())%>
        
<% } %>
        
<br />
        ---------------------------
        
<br />
        降序
<br />
        结果: 
<% IList<int> Arrlist2 = (ViewData["OrderByDes"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int j = 0; j < Arrlist2.Count; j++%>
        
<% { %>
        
<%= Html.Label(Arrlist2[j].ToString())%>
        
<% } %>
        
<br />
        ---------------------------
        
<br />
        自定义方法升序
<br />
        数组:Arrylist={1,-2,3,-4,5,-6,7,-8,9,-10}
<br />
        
<% IList<int> Arrlist3 = (ViewData["CompareList"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int k = 0; k < Arrlist3.Count; k++%>
        
<% { %>
        
<%= Html.Label(Arrlist3[k].ToString())%>
        
<% } %>
         
<br />
        ------------End------------       
    
</h2>
</asp:Content>


Skip

Skip.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Skip
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    
<h2> Skip()、SkipWhile()<br />
        数组Arrylist={1,2,3,4,5,6,7,8,9,10}
<br />
        --------------Begin--------------
<br />
        提取前9个元素,在跳出前小于4的元素,在取出小于5的元素.(如果这里lambda表达式为:M=>M>7,显示不出数据(越界))
<br />
        结果:
<% IList<int> Arrylist = (ViewData["SkipList"as IEnumerable<int>).ToList<int>(); %>
        
<%= Html.Display("<br/>"%>
        
<% for (int i = 0; i < Arrylist.Count; i++%>
        
<% { %>
        
<%= Html.Label(Arrylist[i].ToString()) %>
        
<% } %>
        
<br />
        ------------------------------
<br />
        跳过小于7的元素
<br />
       结果: 
<% IList<int> Arrylist2 = (ViewData["SkipWhile"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int j = 0; j < Arrylist2.Count; j++%>
        
<% { %>
        
<%= Html.Label(Arrylist2[j].ToString())%>
        
<% } %><br />
        ---------------End---------------
</h2>

</asp:Content>


 Take.aspx

Take.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Take
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    
<h2>Take()、TakeWhile()<br />
        数组Arrylist={11, 12, 13, 14, 5, 6, 7, 8, 9, 10 }
<br />
        --------------Begin--------------
<br />
        提取前9个元素,在跳出前小于4的元素,在取出小于7的元素.
<br />
        (如果这里lambda表达式为:M=>M>7,显示不出数据(越界))
<br />
        结果:
        
<% IList<int> Arrylist = (ViewData["TakeList"as IEnumerable<int>).ToList<int>(); %>
        
<%= Html.Display("<br/>"%>
        
<% for (int i = 0; i < Arrylist.Count; i++%>
        
<% { %>
        
<%= Html.Label(Arrylist[i].ToString()) %>
        
<% } %>
        
<br />
        ------------------------------------------------------------
<br />
        提取小于5的元素跳出前3个元素..
<br />
        结果:
        
<% IList<int> Arrylist2 = (ViewData["TakeWhile"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int j = 0; j < Arrylist2.Count; j++%>
        
<% { %>
        
<%= Html.Label(Arrylist2[j].ToString())%>
        
<% } %><br />
        ---------------End---------------
</h2>

</asp:Content>


 

 Func.aspx

Func
<%@ Page Title="" Debug="true" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
    Inherits
="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Func
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
<h2>
        Func():Min,Max,Avg,Sum
<br />
        数组Arrylist={1,2,3,4,5,6,7,8,9,10}
<br />
        --------------Begin--------------
<br />
        结果:
        
<% IList<string> Arrylist = ViewData["Fuc"as List<string>%>
        
<%= Html.Display("<br/>"%>
        
<% for (int i = 0; i < Arrylist.Count; i++%>
        
<% { %>
        
<%= Html.Label(Arrylist[i].ToString()) %>
        
<% } %>
        
<br />
        ---------------End---------------
</h2>
    
<h2>
        Distinct() 数组Arrylist={1,1, 2,2, 3, 3, 4, 5}
<br />
        --------------Begin--------------
<br />
        结果:
        
<% IList<int> Arrylist2 = (ViewData["Distinct"as IEnumerable<int>).ToList<int>(); %>
        
<% for (int j = 0; j < Arrylist2.Count; j++%>
        
<% { %>
        
<%= Html.Label(Arrylist2[j].ToString()) %>
        
<% } %>
        
<br />
        ---------------End---------------
</h2>
</asp:Content>


 

 

 

  That's all.

希望各位园友指点指点。有各位的指点,才会不断进步.

 

VS2008+SP1

Demo 下载

 

 

posted @ 2010-11-17 11:54  子夜.  Views(3623)  Comments(0Edit  收藏  举报