LINQ的范围标识 [英] scope identity with linq

查看:82
本文介绍了LINQ的范围标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何在LINQ中使用诸如范围标识之类的过程来在TODO语句中使用它

How do you a procedure like scope identity with linq want to use it in the TODO statement

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtZip.Text != "" && txtAdd1.Text != "" && txtCity.Text != "")
        {
            TestDataClassDataContext dc = new TestDataClassDataContext();
            Address addr = new Address()
            {
                AddressLine1 = txtAdd1.Text,
                AddressLine2 = txtAdd2.Text,
                City = txtCity.Text,
                PostalCode = txtZip.Text,
                StateProvinceID = Convert.ToInt32(ddlState.SelectedValue)
            };
            dc.Addresses.InsertOnSubmit(addr);
            lblSuccess.Visible = true;
            lblErrMsg.Visible = false;
            dc.SubmitChanges();
     //
     //    TODO: insert new row in EmployeeAddress to reference CurEmp to newly created address
     //
            SetAddrList();
        }
        else
        {
            lblErrMsg.Text = "Invalid Input";
            lblErrMsg.Visible = true;
        }
    }

    protected void SetAddrList()
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        dc.ObjectTrackingEnabled = false;

        var addList = from addr in dc.Addresses
                      from eaddr in dc.EmployeeAddresses
                      where eaddr.EmployeeID == _curEmpID && addr.AddressID == eaddr.AddressID
                      select new
                      {
                          AddValue = addr.AddressID,
                          AddText = addr.AddressID,
                      };
        ddlAddList.DataSource = addList;
        ddlAddList.DataValueField = "AddValue";
        ddlAddList.DataTextField = "AddText";
        ddlAddList.DataBind();
        ddlAddList.Items.Add(new ListItem("<Add Address>", "-1"));
    }

推荐答案

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtZip.Text != "" && txtAdd1.Text != "" && txtCity.Text != "")
        {
            TestDataClassDataContext dc = new TestDataClassDataContext();
            Address addr = new Address()
            {
                AddressLine1 = txtAdd1.Text,
                AddressLine2 = txtAdd2.Text,
                City = txtCity.Text,
                PostalCode = txtZip.Text,
                StateProvinceID = Convert.ToInt32(ddlState.SelectedValue)
            };
            dc.Addresses.InsertOnSubmit(addr);

            dc.SubmitChanges();
            int nAddID = addr.AddressID;
            EmployeeAddress empadd = new EmployeeAddress()
            {
                EmployeeID = Convert.ToInt32(_curEmpID),
                AddressID = nAddID
            };
            dc.EmployeeAddresses.InsertOnSubmit(empadd);
            dc.SubmitChanges();
            lblSuccess.Visible = true;
            lblErrMsg.Visible = false;
            SetAddrList();
        }
        else
        {
            lblErrMsg.Text = "Invalid Input";
            lblErrMsg.Visible = true;
        }
    }

好吧,我已经弄清楚了,我不确定是否那么简单

ok already figured it out I wasn't sure if it was that easy or not

这篇关于LINQ的范围标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆