Linq replace column value with another value
<p>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.</p>

<p>First Code:</p>

<pre><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();
</code></pre>

<p>I searched for some answers, and i found that i can use Select, and make a new object (<a href="https://stackoverflow.com/questions/13894934/linq-replace-null-empty-value-with-another-value">Linq replace null/empty value with another value</a>)</p>

<p>Second Code:</p>

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

<p>And even, Third Code:</p>

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

<p>But only the <strong>First Code works</strong>.</p>

<p>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.</p>