gpt4 book ai didi

c# - 如何在将数据显示到中继器之前对其进行排序?

转载 作者:行者123 更新时间:2023-11-30 21:13:57 25 4
gpt4 key购买 nike

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

网络表单

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

}
}

最佳答案

我会使用泛型:

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

}

关于c# - 如何在将数据显示到中继器之前对其进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6526195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com