在一系列文本框中显示LINQ列表值?

我正在创建一个预订系统,客户可以输入预订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 customers = new List(); List 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); 

您是否尝试在客户列表上使用ToString(),还是仅仅是伪代码?

尝试使用类似的东西: 在C#中将列表转换为字符串

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

 class tblCustomer { public override string ToString() { return this.name; } } 

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

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

然后,定义GetCustomerString方法。

 private string GetCustomerString(tblCustomer customer) { // insert logic or, alternatively, override tblCustomer's ToString instead }