C
C#2mo ago
Saiyanslayer

✅ Better way to set a property using linq?

I have this on a blazor component:
ValueChanged=@( async text => {
protocol.maxIterations = text;
await OnProtocolChanged(protocol);
})
ValueChanged=@( async text => {
protocol.maxIterations = text;
await OnProtocolChanged(protocol);
})
to set a property, I had first set the property on the class and then feed that class to the update method. I want to streamline the process to something similar to how EF Core sets a property in ExecuteUpdate:
ValueChanged=@(async text => await OnProtocolChanged(protocol.SetValue(x => x.maxIterations, text)
ValueChanged=@(async text => await OnProtocolChanged(protocol.SetValue(x => x.maxIterations, text)
looks like EF Core uses this SetPropertyCall class: https://github.com/dotnet/efcore/blob/2e9e879746c3a75ad71b1c3732469c25f01bb8c7/src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs#L3338 https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.query.setpropertycalls-1.setproperty?view=efcore-8.0 When I inspected the class on github, I couldn't find anything substantive that I could parse. The best I could do is this:
ValueChanged=@( async text => await OnProtocolChanged(protocol.GetType().GetProperty("Location"), text))
ValueChanged=@( async text => await OnProtocolChanged(protocol.GetType().GetProperty("Location"), text))
SetPropertyCalls.SetProperty Method (Microsoft.EntityFrameworkCore....
Specifies a property and corresponding value it should be updated to in ExecuteUpdate method.
GitHub
efcore/src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs ...
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. - dotnet/efcore
4 Replies
Joschi
Joschi2mo ago
Why don't you use two way binding?
Saiyanslayer
SaiyanslayerOP2mo ago
I'm looping through values witha foreach loop, IIRC you can't bind those I'll have to try using a for loop instead now that you mention that.
Joschi
Joschi2mo ago
You can, but you need to capture the variable in the loop.
foreach(var item in items){
var capturedItem = item;
<Component @bind="capturedItem'/>
}
foreach(var item in items){
var capturedItem = item;
<Component @bind="capturedItem'/>
}
Saiyanslayer
SaiyanslayerOP2mo ago
thats awesome, thanks!
Want results from more Discord servers?
Add your server