cypherpotato
cypherpotato
CC#
Created by cypherpotato on 11/19/2024 in #help
✅ Why ins't this EF Core query being evaluated client-side?
Currently, I have this query:
C#
var board = Database.KanbanBoards
.Where(k => k.OwnerUserId == AuthUser.OwningId && k.Id == boardId)
.Select(k => new
{
Data =
Database.KanbanColumns
.Where(c => c.ParentBoardId == k.Id)
.OrderBy(c => c.Order)
.Select(c => new
{
column = new
{
c.Id,
c.Color,
c.Label
},
cards = Database.KanbanCards
.Where(a => a.ParentColumnId == c.Id)
.OrderBy(a => a.Order)
.AsEnumerable() // <!--- run the rest on client side
.Select(a => new
{
a.Id,
Title = a.CardTitle,
Info = new
{
a.Value,
a.Temperature,
AssociatedUsers = a.AssociatedUsers
.Select(n => AdapterCacheUnit.FetchUser(n)) // <!-- client side function
.Where(n => n != null)
.Select(n => new
{
n!.UniqueId,
n.UserName
})
}
})
})
.ToArray()
})
.FirstOrDefault();
C#
var board = Database.KanbanBoards
.Where(k => k.OwnerUserId == AuthUser.OwningId && k.Id == boardId)
.Select(k => new
{
Data =
Database.KanbanColumns
.Where(c => c.ParentBoardId == k.Id)
.OrderBy(c => c.Order)
.Select(c => new
{
column = new
{
c.Id,
c.Color,
c.Label
},
cards = Database.KanbanCards
.Where(a => a.ParentColumnId == c.Id)
.OrderBy(a => a.Order)
.AsEnumerable() // <!--- run the rest on client side
.Select(a => new
{
a.Id,
Title = a.CardTitle,
Info = new
{
a.Value,
a.Temperature,
AssociatedUsers = a.AssociatedUsers
.Select(n => AdapterCacheUnit.FetchUser(n)) // <!-- client side function
.Where(n => n != null)
.Select(n => new
{
n!.UniqueId,
n.UserName
})
}
})
})
.ToArray()
})
.FirstOrDefault();
In the query above, in the Database.KanbanCards sub-query I make the rest of the query client-side to transform the "a.AssociatedUsers" column into other information. This is an JSON column.
33 replies
CC#
Created by cypherpotato on 10/8/2024 in #help
Generic converter bounding in EF Core
Is there an way to "easily" define converters for all properties which uses an generic type? Currently, I have something like this:
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<EntityId<User>>().HaveConversion<EntityIdValueConverter<User>>();
configurationBuilder.Properties<EntityId<Tenant>>().HaveConversion<EntityIdValueConverter<Tenant>>();
}
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<EntityId<User>>().HaveConversion<EntityIdValueConverter<User>>();
configurationBuilder.Properties<EntityId<Tenant>>().HaveConversion<EntityIdValueConverter<Tenant>>();
}
and an ugly static method that gets all properties from entities, map eachs one and applies the converter using reflection and activator.
3 replies
CC#
Created by cypherpotato on 10/1/2024 in #help
Why ins't inlining working here?
Just doing an experiment. The following code are not inlined by the compiler:
class Program
{
static void Main(string[] args)
{
int k = DoFoo(10, 20);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int DoFoo(int i, int b)
{
return i + b;
}
}
class Program
{
static void Main(string[] args)
{
int k = DoFoo(10, 20);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int DoFoo(int i, int b)
{
return i + b;
}
}
https://sharplab.io/#gist:692cc1af66c352c2e8138dad6f033959
7 replies
CC#
Created by cypherpotato on 8/27/2024 in #help
How to sustain TcpListener keep-alive?
No description
4 replies
CC#
Created by cypherpotato on 6/4/2024 in #help
can I specify an Type wildcard for GetMethod?
I have both methods:
void DoFoo(string aaa);
void DoFoo(string aaa, string? bbb);
void DoFoo(string aaa);
void DoFoo(string aaa, string? bbb);
I can invoke DoFoo with two parameters through reflection with:
Type[] args = [typeof(string), typeof(string)];
MethodInfo? foo = objType.GetMethod("DoFoo", BindingFlags.Default, args);
Type[] args = [typeof(string), typeof(string)];
MethodInfo? foo = objType.GetMethod("DoFoo", BindingFlags.Default, args);
However, I'm in a scenario where I don't know what one of the types is, because I get them from a collection of objects and through each object I get its type. When an object is null, it is not possible to determine its type, as they are all object?[].
Type[] args = [typeof(string), ???];
Type[] args = [typeof(string), ???];
I needed a way to specify a wildcard in a certain type position. Is there any way to do this?
7 replies
CC#
Created by cypherpotato on 5/29/2024 in #help
✅ Disable trimming for certain assemblies
Hello. There's a way to disable code trimming for certain assemblies? I just want to AOT-Compile my application, but trim only CLR assemblies and not the principal assembly. My code does use a lot of dynamic code, so it would be trimmed on an AOT-compilation. Thank you.
6 replies
CC#
Created by cypherpotato on 5/9/2024 in #help
✅ Difference between explicit and implicit operators
Hello there. What's the difference between defining an implicit and explicit operator? I would like to know the difference between
public static implicit operator JsonValue(JsonArray jarray) => jarray.AsJsonValue();
public static implicit operator JsonValue(JsonArray jarray) => jarray.AsJsonValue();
And
public static explicit operator JsonValue(JsonArray jarray) => jarray.AsJsonValue();
public static explicit operator JsonValue(JsonArray jarray) => jarray.AsJsonValue();
9 replies
CC#
Created by cypherpotato on 4/15/2024 in #help
✅ Disjunctive patterns negation
Hey there. I was wondering why my disjunctive is not working as intended:
authUser.Level
> Internal
authUser.Level is UserLevel.Administrator
> false
authUser.Level is UserLevel.Internal
> true
authUser.Level is not UserLevel.Administrator or UserLevel.Internal
> true
authUser.Level
> Internal
authUser.Level is UserLevel.Administrator
> false
authUser.Level is UserLevel.Internal
> true
authUser.Level is not UserLevel.Administrator or UserLevel.Internal
> true
The last expression result doesn't make sense for me, because I'm checking if authUser.Level is not Administrator or Internal, and it actually is Internal as shown in the first expression, so the result should be false. What i'm doing wrong?
12 replies
CC#
Created by cypherpotato on 10/8/2023 in #help
✅ is there any way to cast Task into Task<object>?
so, i have this delegate function:
public delegate object RouterCallback(HttpRequest request);
public delegate object RouterCallback(HttpRequest request);
which some methods already implements it, and some of then are async that returns Task<something>, and i need that value inside that task. for non Task<T> methods i can have it using:
object actionResult = matchedRoute.Callback(request);
object actionResult = matchedRoute.Callback(request);
and fine, but if it is Task<something> i can only determine if it is an task using:
if (actionResult is Task taskResult)
if (actionResult is Task taskResult)
but it doens't has the .Result property since it's an Task and not Task<T> i can unsafely cast it and get it result using
if (actionResult is Task asyncResult)
{
asyncResult.Wait();

var valueTask = Unsafe.As<Task<object>>(asyncResult);
actionResult = valueTask.Result;
// do something with actionResult
}
if (actionResult is Task asyncResult)
{
asyncResult.Wait();

var valueTask = Unsafe.As<Task<object>>(asyncResult);
actionResult = valueTask.Result;
// do something with actionResult
}
but it only works when T is an reference-type and doens't works when T is an struct/value type object. so, I need to get the value of Task<T> actionResult when actionResult is a Task<T>, but I don't know what <T> is. how do I managed to get it?
using reflection to get the .Result property value requires a lot of resources and costs a lot of performance.
7 replies