1

I am using Entity framework for building an app.

Table to be updated.

[Id] [CheckItemId]  [Checked]  [Comment]
1      100             1          abc
2      150             0          xyz

Class:

public class CheckListRequest
{
    public int ID { get; set; }
    public bool IsChecked { get; set; }
    public string Comment { get; set; }
}

public static void UpdateCheckList(List<CheckListRequest> checkList){

}

How do I update the table multiple column like (Checked,comment) with the values coming in list from frontend using LINQ?

4 Answers 4

6

Assuming dbContext is your DataContext and Id is the primary key of the table, try this:

public static void UpdateCheckList(List<CheckListRequest> checkList)
{
    foreach (CheckListRequest clr in checkList)
    {
        CheckListRequest clrInDB = dbContext.CheckListRequests.SingleOrDefault(o.Id == clr.Id);
        clrInDB.IsChecked = clr.IsChecked;
        clrInDB.Comment = clr.Comment;
    }

    dbContext.SaveChanges();
}
Sign up to request clarification or add additional context in comments.

5 Comments

do the savechanges neeed to come inside the loop to make the changes every time the loop runs...
That depends on your requirements, let's say you update 10 records and the 4th record fails, do you want to rollback the change of the first three records?
You can reduce that .Where(..).SingleOrDefault() to a single SingleOrDefault(...) call by the way ;-)
no rollback is needed even if some record fails it will be logged by other methods.
@ankur in that case you can put dbContext.SaveChanges() inside the foreach loop
0

How do I update the table multiple column like (Checked,comment) with the values

Exactly the same way you'd update only one column...

  1. Get the entity from context
  2. Update column/columns
  3. Call SaveChanges() method on context.
using(var ctx = new MyContext())
{
    var entity = ctx.Table.First(t => t.Id == myId);
    entity.Prop1 = "newValue";
    entity.Prop2 = 23;

    ctx.SaveChanges();
}

1 Comment

there are multiple ids coming , what i meant is there are at least 10 object in the List<CheckListRequest> checkList) ,each object corresponds to a row in database so how do i update multiple columns of the table rows by taking values from the list
0

You can use foreach loop on your list as below:

public static void UpdateCheckList(List<CheckListRequest> checkList)
{
    checkList.foreach(CL =>{
              CheckListRequest objCLR= DataContext.CheckListRequests
              .SingleOrDefault(E => E.Id == CL.Id);
        objCLR.IsChecked = CL.IsChecked;
        objCLR.Comment = CL.Comment;
        });

    DataContext.SaveChanges();
}

Comments

0

You can use ForEachAsync or ForEach like below

var actionTakenOnEndorsement = _context.Endorsement.Where(x => x.IsDeleted == false && x.IsApproved != null).OrderBy(o => o.SolutionId).ThenByDescending(o => o.ModifiedOn).DistinctBy(x => new { x.SolutionId, x.UserId }).AsQueryable();
                
await actionTakenOnEndorsement.ForEachAsync(x => { x.IsEndorsementRevoked = true; x.Comment = "Revoked by system"; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.