zoukankan      html  css  js  c++  java
  • 合并多个List<T>类型并通过LINQ按指定属性排序

    后台CS代码:

    namespace WebFormTest.TestCollect
    {
        public partial class ListTest : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                List<Person> list1 = new List<Person>();
                list1.Add(new Person() { Age = 1, Name = "张三", Time = Convert.ToDateTime("2013/12/20 13:20") });
                list1.Add(new Person() { Age = 5, Name = "李四", Time = Convert.ToDateTime("2013/12/20 14:20") });
                list1.Add(new Person() { Age = 3, Name = "王五", Time = Convert.ToDateTime("2013/12/20 13:40") });
    
                List<Person> list2 = new List<Person>();
                list2.Add(new Person() { Age = 1, Name = "张三2", Time = Convert.ToDateTime("2013/12/20 13:20") });
                list2.Add(new Person() { Age = 5, Name = "李四2", Time = Convert.ToDateTime("2013/12/20 14:20") });
                list2.Add(new Person() { Age = 3, Name = "王五2", Time = Convert.ToDateTime("2013/12/20 13:40") });
    
                list1 = list1.Concat(list2).ToList(); //合并多个list<T>
    
                list1 = list1.OrderByDescending(i => i.Time).ToList(); //降序排列
                //list.OrderBy(i => i.Time);升序排列
    
                Repeater1.DataSource = list1;
                Repeater1.DataBind();
            }
        }
    
    
        public class Person
        {
            private int age;
    
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            private DateTime time;
    
            public DateTime Time
            {
                get { return time; }
                set { time = value; }
            }
    
        }
    }

    前台代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListTest.aspx.cs" Inherits="WebFormTest.TestCollect.ListTest" %>
    
    <!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>
            <table border="1" cellspacing="0" cellpadding="0" width="100%">
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%#Eval("Age") %>
                            </td>
                            <td>
                            <%#Eval("Name") %>
                            </td>
                            <td>
                            <%#Eval("Time") %>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>
        </form>
    </body>
    </html>

    效果展示:

  • 相关阅读:
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业02
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业08
  • 原文地址:https://www.cnblogs.com/zxx193/p/3484195.html
Copyright © 2011-2022 走看看