Home > Blockchain >  Linq / EF in ASP.NET Blazor
Linq / EF in ASP.NET Blazor

Time:11-22

Text

I am using Blazor.

I am trying to get a specific result from dbset.

I have two conditions, salary is 620 and deduction is for p2.

The difficult thing is range, btw 600~700 for salary. Then how I move to specific column to get the result for p2.

var result = await _db.TaxTable.......blah...blah...

In this case, I have to find the result 70

I have tried some code..but I failed everytime.

Could you share the exact knowledge?

Thank you in adv.

CodePudding user response:

Your query into your DbSet should look something like this to get the specific value:

using var dbContext = _factory.CreateDbContext();
dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

var value = await dbContext.Set<TRecord>()
    .SingleOrDefaultAsync(item => x => x.start <= salary && x.end >= salary)
    .select(item => item.p2);

CodePudding user response:

You have to locate the row. Then use a switch statement to get the appropriate value.

double GetDeduction(double salary, string discount)
{
    // You have to include one of the boundaries and exclude the other
    // to avoid overlap. I chose to include start.
    var row = someData.Where(a => salary >= a.start && salary < a.end);

    if(row == null) throw SomeException;

    return discount switch 
    {
        "p1" => row.p1,
        "p2" => row.p2,
        "p3" => row.p3
    }
}
  • Related