Agregar registro múltiple usando Linq-to-SQL

I want to add Multiple rows into Table using 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. moda the record is overwrites with last value of AllList

preguntado el 24 de agosto de 12 a las 08:08

2 Respuestas

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).

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

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.

Respondido 24 ago 12, 10:08

Your link to the MSDN forum actually contradicts your statement somewhat. Yes, you should dispose your data context when you do not need it, but keep it when you do (like the customer table and an orders table example in your link). The correct time to do this will depend on your code, but as a web developer I usually dispose at the end of the web request. - trisped

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

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;            
}

Respondido 24 ago 12, 08:08

Thank you Yograj Gupta. Problem Solved. - Ajay

This helped me as well - karthik shankar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.