我正在创建一个预订系统,客户可以在其中输入预订ID并查看所有其他客人,我需要帮助在一系列文本框中显示我的LINQ列表中的值,我们将不胜感激。< / p>
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);
答案 0 :(得分:1)
您是否尝试在客户列表中使用ToString(),还是仅仅是伪代码?
尝试使用与此类似的内容:Convert a list to a string in C#
这可能包括为tblCustomer类提供ToString()覆盖,然后使用它来提供文本框所需的字符串:
class tblCustomer
{
public override string ToString()
{
return this.name;
}
}
答案 1 :(得分:0)
不确定您要查找的结果 - 是否必须是文本框 - 这意味着,用户可编辑?如果没有,您可以只使用一个标签,并附加每个名称,可以是逗号分隔的字符串,也可以在每个名称之间用“”分隔。可能不是非常漂亮,但它会起作用。而不是'customers'是tblCustomer的列表,使其成为List,并在该循环中添加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
}