我有 3 个具有这样关系的表:
我正在使用 LINQ 插入 SQL。插入到 Customer 表并插入到Customer_OrderTour表后,我可以获得Customer_Id。但是使用List_Visitors表中的Id_visitors列表,我如何获取它们并插入到Customer_OrderTour表(因为Customer_OrderTour和List_Visitors的关系是多对多的)。
这是我的代码:
int customer_id = 0;
#region [Add List Visitors]
private void AddListVisitors(StringCollection sc)
{
List_Visitor add_visitors = new List_Visitor();
var listsToAdd = new List<List_Visitor>();
foreach (string item in sc)
{
if (item.Contains(","))
{
var splitItems = item.Split(",".ToCharArray());
listsToAdd.Add(new List_Visitor() {
Fullname = splitItems[0],
add = splitItems[1],
Sex = splitItems[2]
});
}
}
using (var db = new Travel_DBDataContext())
{
db.List_Visitors.InsertAllOnSubmit(listsToAdd);
db.SubmitChanges();
}
}
#endregion
#region [Get List Visitors]
private void GetListVisitors() {
int rowIndex = 0;
StringCollection sc = new StringCollection();
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox visitor_txtName = (TextBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBox visitor_txtAdd = (TextBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("txtAdd");
DropDownList visitor_ddlSex = (DropDownList)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("ddlSex");
sc.Add(visitor_txtName.Text + "," + visitor_txtAdd.Text + "," + visitor_ddlSex.SelectedItem.Text);
rowIndex++;
}
//Store the current data to StringCollection
AddListVisitors(sc);
}
#endregion
#region [Add New Customer]
private void AddNewCustomer() {
Customer add_customer = new Customer();
add_customer.Fullname = txtName.Text;
add_customer.add = txtAdd.Text;
add_customer.Phone = txtPhone.Text;
add_customer.Email = txtEmail.Text;
db.Customers.InsertOnSubmit(add_customer);
db.SubmitChanges();
customer_id = add_customer.Customer_Id;
}
#endregion
#region [Add Customer Order]
private void AddCustomerOrder (){
Customer_OrderTour cus_order = new Customer_OrderTour();
cus_order.Customer_Id = customer_id;
cus_order.Id_visitors = //What should I do here?
}
#endregion