简体   繁体   English

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

[英]Unable to show datalist items in C#

I am doing my school assignment on a web chat application. 我正在通过网络聊天应用程序完成学校作业。 I wish use a datalist to show the previous chat in a chatbox, but the datalist do not show up. 我希望使用数据列表在聊天框中显示上一个聊天,但该数据列表不会显示。

This is my code in .aspx: 这是我在.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>

And this is my code in .cs: 这是我在.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>;

The as IEnumerable<DataRow> is causing your query variable to be null since the query will result in a collection of anonymous types, not a collection of DataRow objects. as IEnumerable<DataRow>导致query变量为null因为查询将导致匿名类型的集合,而不是DataRow对象的集合。

You'd either need to project to DataRow items or use other means to populate the DataTable . 您要么需要投影到DataRow项,要么使用其他方法来填充DataTable

Also, I would not call .AsEnumerable() on your data sets within the query if it's not necessary. 另外,如果不需要,我不会在查询内的数据集上调用.AsEnumerable() It will force the entire table to be loaded into memory and the join done in Linq rather than on the database. 它将强制将整个表加载到内存中,并在Linq中而不是在数据库上完成连接。 If removing AsEnumerable causes problems then you should find a different way to solve those problems. 如果删除AsEnumerable导致问题,那么您应该找到其他方法来解决这些问题。

I changed my code to this: 我将代码更改为此:

                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; });

and it works, thanks. 它的工作原理,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM