关闭

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

267人阅读 评论(0) 收藏 举报

//前台代码

<%@ 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; }
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44539次
    • 积分:741
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:66篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论