1

私はこのような関係を持つ3つのテーブルを持っています:

ここに画像の説明を入力

LINQ を使用して SQL に挿入しています。Customer テーブルに挿入し、 Customer_OrderTourテーブルに挿入した後、Customer_Idを取得できます。しかし、List_VisitorsテーブルのId_visitorsのリストを使用して、それらを取得してCustomer_OrderTourテーブルに挿入するにはどうすればよいですか ( Customer_OrderTourList_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
4

0 に答える 0