Unmei
Unmei
CC#
Created by Unmei on 2/1/2024 in #help
How to effectively dispose unmanaged resources?
When you "something you created", would it include objects created with using statements? Like using scopedService = serviceProvider.GetRequiredService<IUserService>() or even those are handled "automagically"?
12 replies
CC#
Created by Unmei on 2/1/2024 in #help
How to effectively dispose unmanaged resources?
Got it! I guess it's one less place to look for performance improvements
12 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
it' the value of the variable of the line 3 of this snippet here. One for each user
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
declare username1 varchar(20) = 'foo_bar';
declare dateDisabled1 datetime = '9999-12-31-23:59:59.000';
declare username2 varchar(20) = 'john_doe';
declare dateDisabled2 datetime = '2023-10-12-13:22:37.784';

select * from Orders o
where o.Status = 'CAN' and
(
(o.createdUser = username1 and o.dateDisabled = dateDisabled1) or
(o.createdUser = username2 and o.dateDisabled = dateDisabled2) or
)
order by o.Id;
declare username1 varchar(20) = 'foo_bar';
declare dateDisabled1 datetime = '9999-12-31-23:59:59.000';
declare username2 varchar(20) = 'john_doe';
declare dateDisabled2 datetime = '2023-10-12-13:22:37.784';

select * from Orders o
where o.Status = 'CAN' and
(
(o.createdUser = username1 and o.dateDisabled = dateDisabled1) or
(o.createdUser = username2 and o.dateDisabled = dateDisabled2) or
)
order by o.Id;
it would be something like that. EF generates the query when using ToQueryString() but I don't have the exact query right here. It is almost the same as this one, only differences are what it bring in the select and order by statements
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
in the end, it generate one single query
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
and again, it's many where clauses, not many small queries/selects/sub-selects
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
where clauses are different for each user
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
I will still go back to this and test what happens if I build relations between the tables in EF’s configuration and make the query normally, but multiple where clauses is acceptable since it was a production defect.
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
Not individual queries, but individual “where” clauses for each user. In the tests I made, it would create two variables for each user, their username and they dateDisabled and use them in the where clause. A query filtering by 46 users is executed in roughly 1.2 seconds, which isn’t that bad if compared to the ideal scenario of having the PK/FKs and letting database management figure it out.
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
Thanks for the help. That would be a viable solution too. I was avoiding to try and use Contains since Orders.CreateBy and User.UserName are not PK/FK correlations, thus it could impact performance somehow. Database in this project is all messed up and I can’t change it now 🥲
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
ordersQueryForDebug will render SQL Select statement correctly with the "where" clause just as expected. It also creates all the constants needed for the expressions created, like user.UserName and dateDisabled
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
It Works! So that's the solution:
Expression<Func<OrderSummaryDpid>> usersFilter = order => true == false; // it renders false intentionally;
foreach (var user in users) {
var dateDisabled = user.DateDisabled.HasValue ? user.DateDisabled.Value : DateTime.MaxValue;

Expression<Func<OrderSummaryDpid, bool>> singleUserFilter = order =>
order.CreatedDate <= dateDisabled &&
order.CreateBy == user.UserName;

usersFilter = usersFilter.Or(singleUserFilter); // applies Expression.OrElse to these two
}

var orders = _ordersRepository.Where(usersFilter); // IQueryable<OrderSummaryDpid>
var ordersQueryForDebug = orders.ToQueryString();

return orders.ToList(); // materialize records
Expression<Func<OrderSummaryDpid>> usersFilter = order => true == false; // it renders false intentionally;
foreach (var user in users) {
var dateDisabled = user.DateDisabled.HasValue ? user.DateDisabled.Value : DateTime.MaxValue;

Expression<Func<OrderSummaryDpid, bool>> singleUserFilter = order =>
order.CreatedDate <= dateDisabled &&
order.CreateBy == user.UserName;

usersFilter = usersFilter.Or(singleUserFilter); // applies Expression.OrElse to these two
}

var orders = _ordersRepository.Where(usersFilter); // IQueryable<OrderSummaryDpid>
var ordersQueryForDebug = orders.ToQueryString();

return orders.ToList(); // materialize records
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
Could be like expr1.Combine(BinaryExpression.OrElse, expr2); Can be reduced to expr1.Or(expr2) when chaning the implementation a bit
static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> withPredicate)
{
var invocation = Expression.Invoke(withPredicate, predicate.Parameters);
var combined = Expression.OrElse(predicate.Body, invocation);
return Expression.Lambda<Func<T, bool>>(combined, predicate.Parameters);
}
static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> withPredicate)
{
var invocation = Expression.Invoke(withPredicate, predicate.Parameters);
var combined = Expression.OrElse(predicate.Body, invocation);
return Expression.Lambda<Func<T, bool>>(combined, predicate.Parameters);
}
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
In this gist, this person combines two expressions of same type by a combination which can be a BinaryExpression.Or for example
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
I found a possible solution to this:
30 replies
CC#
Created by Unmei on 12/19/2023 in #help
Is it possible to build a custom Expression<Func<T, bool>> by chaining "Or" operations to use in EF?
it will vary for each user
30 replies