2

Well, im doing a linq query to get a list of results with the same column, and then i need to replace that column value with a new one.

First Code:

var db = GetContext();
var result = from f in GetContext().ProjectStateHistories
             where f.ProjectId.Equals(oldProjectId)
             select f;

foreach (var item in result)
{
     var projectStateHistoryUpdate = db.ProjectStateHistories.Find(item.Id);
     projectStateHistoryUpdate.ProjectId = newProjectId;
     db.Entry(projectStateHistoryUpdate).State = EntityState.Modified;
}
db.SaveChanges();

I searched for some answers, and i found that i can use Select, and make a new object (Linq replace null/empty value with another value)

Second Code:

var result = (from f in GetContext().ProjectStateHistories
              where f.ProjectId.Equals(oldProjectId)
              select f).Select(d=> new { Id = d.Id, EventName = d.EventName, LogUser = d.LogUser, ProjectId = newProjectId, TimeStamp = d.TimeStamp });

And even, Third Code:

var db = GetContext();
var result = (from f in db.ProjectStateHistories
              where f.ProjectId.Equals(oldProjectId)
              select f).Select(d=> new { ProjectId = newProjectId});

But only the First Code works.

I wanted to ask what i am doing wrong, since i think it is better to change the value with a query, instead of using a foreach.

13
  • 2nd and 3rd, you are reading the data, not updating it back ! Commented May 6, 2016 at 14:39
  • What's wrong with the first code fragment? Why do yo want to do it differently? Commented May 6, 2016 at 14:41
  • 1. do you really need to SaveChanges in the foreach loop?.2. Why are you calling GetContext() again in the query instead of using db? Commented May 6, 2016 at 14:41
  • @GertArnold first code is working great, but i wanted to avoid the foreach. Commented May 6, 2016 at 14:41
  • @RobertoDeLaParra yah, savechanges must be out of the foreach, your are right thanks. About the GetContext, i m using db, my fault sorry. Commented May 6, 2016 at 14:47

3 Answers 3

5

See code below:

var db = GetContext();
(from f in db.ProjectStateHistories
     where f.ProjectId.Equals(oldProjectId)
     select f)
       .ToList()
       .ForEach(i => i.ProjectId = newProjectId);
db.SaveChanges();

Alternatively:

var db = GetContext();
db.ProjectStateHistories
       .Where(f => f.ProjectId.Equals(oldProjectId))
       .ToList()
       .ForEach(f => f.ProjectId = newProjectId);
db.SaveChanges();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @Riad ! First Code worked, what do you think is better to use ? The first one, or the second one ?
@pierregomes This too has a for each loop in it ;) Its just refactored ;)
I prefer the method notation (the second one). I find it easier to read than query notation.
2

The shortest way I know of to replace your code is this:

var db = getcontext();

db.ProjectStateHistories
    .Where(f => f.ProjectId.Equals(oldProjectId))
    .ToList()
    .ForEach(f => f.ProjectId = newProjectId);

db.SaveChanges();

Other answers can be found here

1 Comment

Sorry for not answering. This works too :) Thanks a lot for your time!
0

I've just had a thought that could help you, I am just free coding here!

If you just put the for each as part of the select, and then save your changes will that work?

foreach (var source in db.ProjectStateHistories.Where(x => x.ProjectId == oldProjectId)) 
{ 
     source.ProjectId= newProjectId; 
     db.Entry(source).State = EntityState.Modified; 
} 
db.SaveChanges();

I think this is a more efficient way of doing it.

Also the .Select() method is only really useful if you need to Project to a view Model, it won't change the variables in the database, just show them in the newly declared object.

Thanks,

Phill

2 Comments

Thanks a lot @Phillip :) Then there is no change between your code and Riad's code ? Because yours it's easier to read. That's a nice solution too. I made this question because i thought i should avoid foreach
I am using your code, because it is easier to read :) Thanks a lot for your time.

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.