Home > OS >  How to provide this code. Linq code i want simplify
How to provide this code. Linq code i want simplify

Time:03-29

I have this code:

dl.db.Table.First(x => x.Id == T.Id).Column1 = "a";
dl.db.Table.First(x => x.Id == T.Id).Column2 = "b"; 
dl.db.Table.First(x => x.Id == T.Id).Column3 = "c"; 
dl.db.Table.First(x => x.Id == T.Id).Column4 = "d"; 
dl.db.Table.First(x => x.Id == T.Id).Column5 = "e"; 

I want make something like this but i cannot

dl.db.Table.First(x => x.Id == T.Id){    
   Column1 = "a";    
   Column2 = "b";    
   Column3 = "c";      
   Column4 = "d";    
   Column5 = "e"; 
}

CodePudding user response:

First get the element from your table and store it into a varible, then modify that variable:

var e = dl.db.Table.First(x => x.Id == T.Id);
e.Column1 = "a";
e.Column2 = "b"; 
e.Column3 = "c"; 
e.Column4 = "d"; 
e.Column5 = "e"; 

CodePudding user response:

I think the only way you have is to use with expression. Some thing like this:

var p1 = new Person("Name1", 5);
var p2 = p1 with { Name = "Name2" };

record Person(string Name, int Age);
  • Related