C# 使用 Linq-to-SQL 添加多条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12104952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:04:25  来源:igfitidea点击:

Add Multiple record using Linq-to-SQL

c#sql-server-2008linq-to-sql

提问by Ajay

I want to add Multiple rows into Table using Linq to SQL

我想使用 Linq to SQL 将多行添加到表中

    public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
    public static bool Insert_Question_Answer(List<QuestionClass.Tabelfields> AllList)
    {
          Feedback f = new Feedback();
          List<Feedback> fadd = new List<Feedback>();
            for (int i = 0; i < AllList.Count; i++)
            {
                f.Email = AllList[i].Email;
                f.QuestionID = AllList[i].QuestionID;
                f.Answer = AllList[i].SelectedOption;
                fadd.Add(f);
            }
            context.Feedbacks.InsertAllOnSubmit(fadd);
            context.SubmitChanges();
        return true;            
    }

When I add records into list object i.e. faddthe record is overwrites with last value of AllList

当我将记录添加到列表对象中时,即fadd记录被AllList 的最后一个值覆盖

采纳答案by Adam

I'm late to the party, but I thought you might want to know that the for-loop is unnecessary. Better use foreach (you don't need the index).

我迟到了,但我想你可能想知道 for 循环是不必要的。更好地使用 foreach(您不需要索引)。

It gets even more interesting when you use LINQ (renamed method for clarity):

当您使用 LINQ(为了清晰起见,重命名方法)时,它会变得更加有趣:

public static void InsertFeedbacks(IEnumerable<QuestionClass.Tabelfields> allList)
{
    var fadd = from field in allList
               select new Feedback
                          {
                              Email = field.Email,
                              QuestionID = field.QuestionID,
                              Answer = field.SelectedOption
                          };
    context.Feedbacks.InsertAllOnSubmit(fadd);
    context.SubmitChanges();
}

By the way, you shouldn't keep one data context that you access all the time; it's better to create one locally, inside a using statement, that will properly handle the database disconnection.

顺便说一句,你不应该保留一个你一直访问的数据上下文;最好在 using 语句中在本地创建一个,以正确处理数据库断开连接。

回答by Yograj Gupta

You should create object of Feedback in the scope of for loop, so change your method to :

您应该在 for 循环范围内创建反馈对象,因此将您的方法更改为:

public static bool Insert_Question_Answer(List<QuestionClass.Tabelfields> AllList)
{
      List<Feedback> fadd = new List<Feedback>();
        for (int i = 0; i < AllList.Count; i++)
        {
            Feedback f = new Feedback();
            f.Email = AllList[i].Email;
            f.QuestionID = AllList[i].QuestionID;
            f.Answer = AllList[i].SelectedOption;
            fadd.Add(f);
        }
        context.Feedbacks.InsertAllOnSubmit(fadd);
        context.SubmitChanges();
    return true;            
}