Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)


ID:  

1

2

3



InsertTable:  

first insert:  1  12  Flower  A

two insert  :  1  12  Flower  A
               2  12  Flower  A

third insert:  1  12  Flower  A
               2  12  Flower  A
               3  12  Flower  A

Result:
               1  12  Flower  A

               1  12  Flower  A

               2  12  Flower  A

               1  12  Flower  A

               2  12  Flower  A

               3  12  Flower  A

I want to Result :  1   12  Flower  A

                    2   12  Flower  A

                    3   12  Flower  A


What I have tried:

var query=MyStudentEntities.Student.OrderBy(x=>x.ID).Where(x=>x.ClassID="12").GroupBy(x=>x.ID).Distinct();


foreach(var item in query)
{
    students.Add(new TABLEA()
{
  ID=item.ID,
  ClassID=12,
  ClassName="Flower",
  ClassBranch="A"
});

MyStudentEntities.TABLEA.AddRange(students);
MyStudentEntities.SaveChanges();
}
Posted
Updated 18-Dec-19 2:40am
v2
Comments
ZurdoDev 17-Dec-19 14:08pm    
This is going to get closed very quickly unless you ask a question.
Member 14169626 17-Dec-19 14:15pm    
why I double-lost in the query I made him I want to add as 1 2 3
ZurdoDev 18-Dec-19 7:37am    
What does that mean?
Maciej Los 17-Dec-19 14:56pm    
Did you check what the below query returns?
var query=MyStudentEntities.Student.OrderBy(x=>x.ID).Where(x=>x.ClassID="12").GroupBy(x=>x.ID).Distinct();
Simon_Whale 17-Dec-19 15:38pm    
From looking at this I have one question. are you inserting all these rows each time or are you just trying to add a "new row" each call?

1 solution

Try to change your code as follow:

C#
var data=MyStudentEntities.Student
    .Where(x=>x.ClassID==12)
    .GroupBy(x=>x.ID)
    .Select(grp=>new TABLEA()
        {
          ID=grp.Key,
          ClassID=12,
          ClassName="Flower",
          ClassBranch="A"
        })
    .ToList();

MyStudentEntities.TABLEA.AddRange(data);
MyStudentEntities.SaveChanges();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900