1
Follows code:
using (var db = new Entities())
{
var result = db
.Tabela1
.Select(x => new
{
x.Coluna1,
x.Coluna2
})
.ToList();
foreach (var item in result)
{
if (item.Coluna1 == string.Empty)
{
item.Coluna1 = "Novo Valor";
}
}
db.SaveChanges();
}
Within the if
, get error:
The property or indexer ". Column1" cannot be assigned, as it is read-only
Follow another code below (Works with the class "Suaclasse"):
using (var db = new Entities())
{
var result = db
.Tabela1
.Select(x => new SuaClasse //Aqui
{
x.Coluna1,
x.Coluna2
})
.ToList();
foreach (var item in result)
{
if (item.Coluna1 == string.Empty)
{
item.Coluna1 = "Novo Valor";
}
}
db.SaveChanges();
}
Claase:
public class SuaClasse
{
public string Coluna1 { get; set; }
public string Coluna2 { get; set; }
}
You can make it work without creating the class (example in the first code) ?
There is no other way without creating the class ? Type
Tuple
?– Matheus Miranda
Maybe using a tuple, but I don’t think it’s a good idea in this case, if you’re going to touch the object, create a specific type. Neither anonymous type nor tuple were created for this.
– Maniero
So the best way to modify a foreach’s value is to create a new class ?
– Matheus Miranda
The way you did in the second code.
– Maniero