如何在将数据显示到转发器之前对其进行排序?

时间:2011-06-29 19:33:58

标签: c# linq-to-sql

如何在将数据显示到转发器之前对数据进行排序 我编写了下面的代码,并想知道如何按姓氏排序数据,而不依赖于sql server。 我很想通过LINQ对它进行排序,这是可能的。你能给我一个关于如何根据我的代码对数据进行排序的例子。

web窗体

<%@ 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>
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table cellspacing="1"  border="1">
                    <tr>
                        <td>First Name</td>
                        <td>Last Name</td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%# DataBinder.Eval(Container.DataItem, "FirstName") %> </td>
                    <td><%# DataBinder.Eval(Container.DataItem, "LastName") %> </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
</body>
</html>

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public class OFullName
{
    private string firstName;
    public string FirstName
    {
        get
        {
            return firstName;
        }
    }
    private string lastName;
    public string LastName
    {
        get
        {
            return lastName;
        }
    }
    public OFullName(string FirstName, string LastName)
    {
        firstName = FirstName;
        lastName = LastName;
    }
}

public partial class _Default : System.Web.UI.Page
{
    public static Random random = new Random();// The random number sequence
    private static string ShuffleString(string StringInput)
    {
             //Create new string from the reordered char array
            string rand = new string(StringInput.ToCharArray().
            OrderBy(s => (random.Next(2) % 2) == 0).ToArray());
        return rand;
    }

    protected void Page_Load(object sender, EventArgs e)
    {


        ArrayList xNames = new ArrayList();
        string TestString = "123ABCDEFGHIJK";
        for (int i = 1; i <= 10; i++)
        {
            xNames.Add(new OFullName(ShuffleString(TestString), ShuffleString(TestString)));
        }

        Repeater1.DataSource = xNames;//Add data from the Array list
        //How to sort data?
        Repeater1.DataBind();//Bind data

    }
}

2 个答案:

答案 0 :(得分:2)

按姓氏排序:

    Repeater1.DataSource = xNames.OrderBy(item => item.LastName);
    Repeater1.DataBind();

答案 1 :(得分:2)

我会使用泛型:

protected void Page_Load(object sender, EventArgs e)
{


    var xNames = new List<OFullName>();
    string TestString = "123ABCDEFGHIJK";
    for (int i = 1; i <= 10; i++)
    {
        xNames.Add(new OFullName(ShuffleString(TestString), ShuffleString(TestString)));
    }

    Repeater1.DataSource = xNames.OrderBy(x => x.LastName); // sorts by last name Ascending. Use OrderByDesc to sort descending.

    Repeater1.DataBind();//Bind data

}