-1

我正在创建一个预订系统,客户可以在其中输入预订 ID 并查看所有其他客人参加,我需要帮助在一系列文本框中显示我的 LINQ 列表中的值,任何和所有帮助将不胜感激。

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

namespace BookingCustomers
{
    public partial class BookingGuests : System.Web.UI.Page
    {
        private HotelConferenceEntities datacontext = new HotelConferenceEntities();
        private void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                try
                {
                    int id = int.Parse(BookID.Text.ToString());
                    tblBooking booking = datacontext.tblBookings.SingleOrDefault(x => x.BookingID == id);
                    tblVenue venue = datacontext.tblVenues.SingleOrDefault(x => x.VenueID == booking.Venue);
                    List<tblCustomer> customers = new List<tblCustomer>();
                    List<tblBookingGuest> guests = booking.tblBookingGuests.ToList();

                        foreach (tblBookingGuest guest in guests)
                        {
                            tblCustomer newcust = datacontext.tblCustomers.SingleOrDefault(x=>x.CustomerID == guest.CustomerID);
                            customers.Add(newcust);
                        }
                        int count = customers.Count;
                        CustFirstName.Text = Convert.ToString(customers);
4

2 回答 2

1

您是否尝试在客户列表上使用 ToString(),或者这只是伪代码?

尝试使用类似的东西:Convert a list to a string in C#

这可能包括为 tblCustomer 类提供一个 ToString() 覆盖,然后使用它为您的文本框提供所需的字符串:

class tblCustomer
{
    public override string ToString()
    {
        return this.name;
    }
}
于 2013-05-30T12:21:22.627 回答
0

不确定您正在寻找什么结果 - 它们必须是文本框 - 意思是用户可编辑的吗?如果没有,您可以只使用一个标签,并附加每个名称,可以是逗号分隔的字符串,也可以在每个名称之间用“”分隔。可能不是非常漂亮,但它会工作。而不是 'customers' 是 tblCustomer 的列表,将其设为列表,并在该循环中添加 ToString 方法,然后为文本框执行 string.join。

List<string> customers = new List<string>();
foreach (tblBookingGuest guest in guests)
{
   tblCustomer newCust = datacontext.tblCustomers.SingleOrDefault(x=>x.CustomerID == guest.CustomerID);
   customers.Add(GetCustomerString(newCust));
}
CustFirstName.Text = string.Join("</BR>", customers.ToArray);

稍后,定义 GetCustomerString 方法。

private string GetCustomerString(tblCustomer customer)
{
   // insert logic or, alternatively, override tblCustomer's ToString instead
}
于 2013-05-30T12:20:05.180 回答