C#C
C#2y ago
Angius

Is there any way to *conditionally* set a property with EF `.ExecuteUpdateAsync()`?

Right now, I'm doing
var thing = await _ctx.Things.FirstOrDefaultAsync(t => t.Id == req.Id);
if (thing.Name != req.Name) thing.Name = req.Name;
if (thing.Count != req.Count) thing.Count = req.Count;
await _ctx.SaveChangesAsync();

but I'm not sure how would I do something similar, as in, something that avoids setting x.Name = x.Name, with the Execute method. Something like
var rows = await _ctx.Things.Where(t => t.Id == req.Id)
    .ExecuteUpdateAsync(props => props
        .SetPropertyIf(t => t.Name != req.Name, t => t.Name, req.Name)
        .SetPropertyIf(t => t.Count != req.Count, t => t.Count, req.Count))

but no such thing seems to exist.
Was this page helpful?