Linq 排序的两种方法(ASP.net)

//前台代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server" id="Div1">
   
    </div>
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

 

 

//后台代码

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;


public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ////第一种方式
        //List<listModel> plist = new List<listModel>
        //{
        //    new listModel { Key = "11", Value = "a" },
        //     new listModel { Key = "33", Value = "c" },
        //      new listModel { Key = "22", Value = "b" }
        //};

        //var p = from o in plist orderby o.Key ascending select o;

        //foreach (var s in p)
        //{
        //    Div1.InnerHtml += s.Value + "\n";
        //}

        //第二种方式
        List<listModel> plist = new List<listModel>
        {
            new listModel { Key = "11", Value = "a" },
             new listModel { Key = "33", Value = "c" },
              new listModel { Key = "22", Value = "b" }
        };
        var l = plist.OrderByDescending(k => k.Key);
        foreach (var s in l)
        {
            Div1.InnerHtml += s.Value + "\n";
        }

    }
    public class listModel
    {
        private string key;

        public string Key
        {
            get { return key; }
            set { key = value; }
        }
        private string value;

        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    }
}

阅读更多
个人分类: c#
上一篇dotNetFlexGrid-asp.net的异步表格控件 使用指南
下一篇LINQ 首部曲 : LINQ To Object Part 1
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭