无法在 C# 中显示数据列表项

Unable to show datalist items in C#

我正在通过网络聊天应用程序完成学校作业。我想用数据列表在聊天框中显示之前的聊天记录,但是数据列表没有显示。

这是我在 .aspx 中的代码:

<asp:DataList ID="DataList3" runat="server">
    <ItemTemplate runat="server">
        <div class="lv-item media">
            <div class="lv-avatar pull-left">
                <asp:Image ID="Image4" runat="server" ImageUrl='<%# Bind("Image") %>' />

            </div>
            <div class="media-body">
                <div class="ms-item">
                    <span class="glyphicon glyphicon-triangle-left" style="color: #000000;"></span>
                    <asp:Label ID="Message" runat="server" Text='<%# Bind("Message") %>'></asp:Label>
                </div>
                <small class="ms-date"><span class="glyphicon glyphicon-time"></span>&nbsp;
                    <asp:Label ID="Date" runat="server" Text='<%# Bind("Time") %>'></asp:Label></small>
            </div>
        </div>
    </ItemTemplate>
</asp:DataList>

这是我在 .cs 中的代码:

public void LoadChatbox()
{
    Session["Name"] = "weiwei";
    int waiwai = 1004;
    //Request.QueryString["friendid"] = "waiwai";
    Session["userid"] = 1005;

    int userid = int.Parse(Session["userid"].ToString());
    //int receiverid = int.Parse(Request.QueryString["friendid"]);
    int receiverid = waiwai;
    string name = Session["Name"].ToString();
    DataSet ds = new DataSet();
    ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
    IEnumerable<DataRow> query =
    (from user in db.Users.AsEnumerable()
     join ch in db.ChatHistories.AsEnumerable()
         on user.Userid equals ch.Senderid
     where ch.Senderid == userid && ch.Receiverid == receiverid || ch.Receiverid == userid && ch.Senderid == receiverid orderby ch.id
     select new
     {
         Image = user.image,
         Message = ch.message,
         Time = ch.dateTime
     })
    as IEnumerable<DataRow>;

    DataTable chatbox = query.CopyToDataTable<DataRow>();
    ds.Tables.Add(chatbox);
    DataList3.DataSource = ds;
    DataList3.DataBind();
}
 select new
 {
     Image = user.image,
     Message = ch.message,
     Time = ch.dateTime
 })
as IEnumerable<DataRow>;

as IEnumerable<DataRow> 导致您的 query 变量为 null,因为查询将生成匿名类型的集合,而不是 DataRow 对象的集合。

您需要投射到 DataRow 项目或使用其他方式填充 DataTable

此外,如果没有必要,我不会在查询中对您的数据集调用 .AsEnumerable()。它将强制将整个 table 加载到内存中,并在 Linq 中而不是在数据库中完成连接。如果删除 AsEnumerable 会导致问题,那么您应该找到其他方法来解决这些问题。

我将代码更改为:

                var chatbox = new DataTable();
        chatbox.Columns.Add("Image", typeof(string));
        chatbox.Columns.Add("Message", typeof(string));
        chatbox.Columns.Add("Time", typeof(DateTime));
        (from user in db.Users.AsEnumerable()
         join ch in db.ChatHistories.AsEnumerable()
             on user.Userid equals ch.Senderid
         where ch.Senderid == userid && ch.Receiverid == receiverid || ch.Receiverid == userid && ch.Senderid == receiverid
         orderby ch.id
         select new
         {
             Image = user.image,
             Message = ch.message,
             Time = ch.dateTime
         })
         .Aggregate(chatbox, (dt, r) => { dt.Rows.Add(r.Image, r.Message, r.Time); return dt; });

成功了,谢谢。