如何获取' id'插入LINQ后的字段?

时间:2013-06-26 18:31:02

标签: c# asp.net .net linq linq-to-sql

我正在使用此代码在Gridview中动态创建行,我正在使用LINQ将这些行插入到DB中。现在我想在插入数据库后返回ID列表,但我不知道如何。

这是我的代码:

#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");
                    //ASPxComboBox visitor_cbxSex = (ASPxComboBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("cbxSex");

                    sc.Add(visitor_txtName.Text + "," + visitor_txtAdd.Text);
                    rowIndex++;
                }
                //Store the current data to StringCollection
                AddListVisitors(sc);
            }
}
#endregion

#region [Add List Visitors]
private void AddListVisitors(StringCollection sc)
{
    try {
        List_Visitor add_visitors = new List_Visitor();
        string[] splitItems = null;
        foreach (string item in sc)
        {
            if (item.Contains(","))
            {
                Travel_DBDataContext db = new Travel_DBDataContext();
                splitItems = item.Split(",".ToCharArray());
                add_visitors.Fullname = splitItems[0];
                add_visitors.add = splitItems[1];
                db.List_Visitors.InsertOnSubmit(add_visitors);
                db.SubmitChanges();
            }

        }      
    }
    catch (Exception addvisitors_error) {
        Response.Write(addvisitors_error.Message + "_method addlistvisitor");
    }
}
#endregion

2 个答案:

答案 0 :(得分:1)

调用SubmitChanges后,add_visitors对象将包含新插入行的主键。

为了获得所有ID,建议进行一些重构:

// First build an in memory list of objects to add.
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]
    });
  }
}

// Then add all to the db in one call. Wrap in using block to
// propery dispose of the DataContext and underlying connection.
using(var db = new Travel_DBDataContext())
{
  db.List_Visitors.InsertAllOnSubmit(listsToAdd);
  db.SubmitChanges();
}

// At this point listsToAdd contains all added lists with the generated
// primary key.

答案 1 :(得分:0)

正如Anders Abel所说,一旦你调用了SubmitChanges,Id就会在已经插入的对象中更新,但是你需要保留对这些对象的引用,现在你正在使用一个不能工作的对象。

#region [Add List Visitors]
private void AddListVisitors(StringCollection sc)
{
    try {
        var newVisitors = new List<List_Visitor>();
        string[] splitItems = null;
        foreach (string item in sc)
        {
            if (item.Contains(","))
            {
                Travel_DBDataContext db = new Travel_DBDataContext();
                splitItems = item.Split(",".ToCharArray());
                var add_visitors = new List_Visitor();
                add_visitors.Fullname = splitItems[0];
                add_visitors.add = splitItems[1];
                db.List_Visitors.InsertOnSubmit(add_visitors);

                newVisitors.Add(add_visitors);
            }
        }    
        db.SubmitChanges();
        // Now newVisitors will contain a collection of List_Visitor
        // objects with the new Id's in.  
    }
    catch (Exception addvisitors_error) {
        Response.Write(addvisitors_error.Message + "_method addlistvisitor");
    }
}
#endregion