Sernik
Sernik
Explore posts from servers
CC#
Created by Sernik on 10/26/2022 in #help
Keyset pagination with filters
Hello everyone! I use this piece of code (probably very inefficient piece of code) to get employees from database using keyset pagination.
public IEnumerable<EmployeeView> GetEmployees()
{
var amount = _appState.ItemsPerPage;

var lastId = _appState.LastEmployeeId ?? 0;

var employees = _context.Employees
.AsNoTracking()
.OrderBy(e => e.EmpNo)
.Where(e => e.EmpNo > lastId);

#region filters
if (_appState.GenderFilter.GetAllowedValue(out var allowedGender))
employees = employees.Where(e =>
e.Gender[0] == allowedGender);

if (_appState.DepartmentFilter.GetAllowedValue(out var allowedDepartment))
employees = employees.Where(e =>
_departmentService.GetDepartmentName(e.EmpNo) == allowedDepartment);

if (_appState.SalaryFilter.GetAllowedValue(out var allowedSalary))
employees = employees.Where(e =>
_salaryService.GetSalaryValue(e.EmpNo) >= allowedSalary.From &&
_salaryService.GetSalaryValue(e.EmpNo) <= allowedSalary.To);
#endregion filters

employees = employees.Take(amount);

_appState.LastEmployeeId = employees.Last().EmpNo;

return employees.ToList().Select(e => new EmployeeView(
FirstName: e.FirstName,
LastName: e.LastName,
IsMale: e.Gender == "M",
DepartmentName: _departmentService.GetDepartmentName(e.EmpNo),
JobTitle: _titleService.GetTitleName(e.EmpNo),
Salary: _salaryService.GetSalaryValue(e.EmpNo)
));
}
public IEnumerable<EmployeeView> GetEmployees()
{
var amount = _appState.ItemsPerPage;

var lastId = _appState.LastEmployeeId ?? 0;

var employees = _context.Employees
.AsNoTracking()
.OrderBy(e => e.EmpNo)
.Where(e => e.EmpNo > lastId);

#region filters
if (_appState.GenderFilter.GetAllowedValue(out var allowedGender))
employees = employees.Where(e =>
e.Gender[0] == allowedGender);

if (_appState.DepartmentFilter.GetAllowedValue(out var allowedDepartment))
employees = employees.Where(e =>
_departmentService.GetDepartmentName(e.EmpNo) == allowedDepartment);

if (_appState.SalaryFilter.GetAllowedValue(out var allowedSalary))
employees = employees.Where(e =>
_salaryService.GetSalaryValue(e.EmpNo) >= allowedSalary.From &&
_salaryService.GetSalaryValue(e.EmpNo) <= allowedSalary.To);
#endregion filters

employees = employees.Take(amount);

_appState.LastEmployeeId = employees.Last().EmpNo;

return employees.ToList().Select(e => new EmployeeView(
FirstName: e.FirstName,
LastName: e.LastName,
IsMale: e.Gender == "M",
DepartmentName: _departmentService.GetDepartmentName(e.EmpNo),
JobTitle: _titleService.GetTitleName(e.EmpNo),
Salary: _salaryService.GetSalaryValue(e.EmpNo)
));
}
This code works perfectly fine unless i activate filters. I get an exception that ef core can't translate my shitty Linq to SQL and that's kinda understandable. But the question is, how can I make this work? Afaik loading all entities to list, filtering and then taking, for example 50 entities isn't a good idea.
19 replies
CC#
Created by Sernik on 10/24/2022 in #help
Await Linq Expression
9 replies
CC#
Created by Sernik on 10/17/2022 in #help
Deserialize JSON to dynamic object without Newtonsoft.Json
Hi, is there a way to deserialize json object to dynamic in .net? ideally from HttpContent. I don't want to use additional dependency, but i can't find vanilla solution. Thanks for help!
40 replies
CC#
Created by Sernik on 10/17/2022 in #help
Get data from api periodically
Hi everyone, I am making a blazor server web app, and i have to get data from api every 10 seconds and save it to database and update index.razor page with that data. The question is, how can i get that data? I have to create service with timer and add it to services in program.cs? and which timer should i use? Thanks for help!
5 replies
CC#
Created by Sernik on 10/6/2022 in #help
Collapse|Expand all nodes in tree
5 replies
CC#
Created by Sernik on 8/16/2022 in #help
regex help [Answered]
So I made a regex with little help of copilot, it looks like this:
^
(?<source>[0-7],[0-7])\s?
(?<destination>[0-7],[0-7])\s?
(?<capture>[0-7],[0-7])?
$
^
(?<source>[0-7],[0-7])\s?
(?<destination>[0-7],[0-7])\s?
(?<capture>[0-7],[0-7])?
$
It should match strings like this: "n,n > n,n x n,n" or like this: "n,n > n,n", where n is number between 0 and 7 (spaces between are optional) However when I test it here https://regex101.com/r/MMnQql/1 I get no matches The question is why? Can somebody help me?
14 replies