Updating properties and states 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&lt;MyObjectproperties&gt; properties)
    {
        var propertiesToBeUpdated = new List&lt;MyObjectproperties&gt;();
        //I am convinced I can do this in one linq statement
        foreach (var output in properties)
        {
            var outputToBeUpdated = _context.MyObjectProperties.FirstOrDefault(x =&gt; 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&lt;MyObjectProperties&gt; 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).
var propertiesToBeUpdated = properties.Select(p =&gt; {
    var prop = _context.MyObjectProperties.FirstOrDefault(x =&gt; x.Key == output.Key);

    return prop == null ? null :
        new MyObjectProperties {
            Key = prop.propertiesKey,
            Name = prop.propertiesName,
            Value = prop.propertiesValue
        };
}).Where(p =&gt; p != null);


The extra set of {} inside your LINQ statement allow you to do multiple lines. If for some reason, you're going to be absolutely POSITIVE that prop would never be null, it would simplify the LINQ a lot more by not having to return nulls (since you have to return SOMETHING for each item in the collection you're iterating). You also then wouldn't need the Where() at the end as well.

UPDATE

If it will never be null, then it is even simpler:

var propertiesToBeUpdated = properties.Select(p =&gt; {
    var prop = _context.MyObjectProperties.First(x =&gt; x.Key == output.Key);

    return new MyObjectProperties {
            Key = prop.propertiesKey,
            Name = prop.propertiesName,
            Value = prop.propertiesValue
        };
});