I am rewriting a VB.NET application in C#. This code was originally done in 50 lines and I am trying to clean it up. As you can see I have gotten it down to 10 but I still feel its ugly. I want to convert the call and assignments into one Linq statement or method chain. See Comments:
public void Update(IEnumerable<MyObjectproperties> properties)
{
var propertiesToBeUpdated = new List<MyObjectproperties>();
//I am convinced I can do this in one linq statement
foreach (var output in properties)
{
var outputToBeUpdated = _context.MyObjectProperties.FirstOrDefault(x => x.Key == output.Key);
outputToBeUpdated.propertiesName = output.propertiesName;
outputToBeUpdated.propertiesValue = output.propertiesValue;
propertiesToBeUpdated.Add(outputToBeUpdated);
}
_context.Entry(propertiesToBeUpdated).State = EntityState.Modified;
_context.SaveChanges();
}
What I am hoping this method looks like when I am done is:
public void Update(IEnumerable<MyObjectProperties> properties)
{
var propertiesToBeUpdated = //My new linq statement here
_context.Entry(propertiesToBeUpdated).State = EntityState.Modified;
_context.SaveChanges();
}
I am "okay" at writing complex linq statements but this one is a bit more than I can handle (assuming its possible).