3
\$\begingroup\$

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).

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$
var propertiesToBeUpdated = properties.Select(p => {
    var prop = _context.MyObjectProperties.FirstOrDefault(x => x.Key == output.Key);

    return prop == null ? null :
        new MyObjectProperties {
            Key = prop.propertiesKey,
            Name = prop.propertiesName,
            Value = prop.propertiesValue
        };
}).Where(p => 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 => {
    var prop = _context.MyObjectProperties.First(x => x.Key == output.Key);

    return new MyObjectProperties {
            Key = prop.propertiesKey,
            Name = prop.propertiesName,
            Value = prop.propertiesValue
        };
});
\$\endgroup\$
3
  • \$\begingroup\$ I am certain that it would never be null. \$\endgroup\$
    – Robert
    Commented Jul 24, 2014 at 17:27
  • \$\begingroup\$ I would give you an extra +1 if I could. I was not aware you could add curly braces to allow for extra code lines in a single linq query. \$\endgroup\$
    – Robert
    Commented Jul 24, 2014 at 18:14
  • \$\begingroup\$ That's true of all delegates. \$\endgroup\$
    – krillgar
    Commented Jul 24, 2014 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.