© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
19 replies
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();
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
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))
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Is there any benefit to write a Property with a private variable ?
C#CC# / help
4y ago
❔ Is there any way to use a proxy with TestServer?
C#CC# / help
4y ago
✅ Better way to set a property using linq?
C#CC# / help
16mo ago
❔ Is there any elegant way to generate a SHA256 hash ?
C#CC# / help
4y ago