HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Updating properties and states

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
propertiesstatesupdatingand

Problem

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 properties)
    {
        var propertiesToBeUpdated = new List();
        //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 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).

Solution

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
        };
});

Code Snippets

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);
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
        };
});

Context

StackExchange Code Review Q#57938, answer score: 2

Revisions (0)

No revisions yet.