✅ How can I improve this code?
It seems that I should change the way tags are stored. Or should I not?
16 Replies
I don't really understand why you constantly reassign notes
Your query already includes the filters to filter on the DB side
That's perfect
Can you maybe include all the filters at once from your loop?
You currently have a lot of "wasted" code, you're potentially iterating a few times through your foreach, you query the DB for every loop, and constantly reassign notes, essentially throwing away the result from the last iteration
So you should either check if you can include all filters in one or maybe only a few queries, or you need probably want to Add all the results to a List instead of throwing them away
Right now you might query the DB e.g. 50 times, but essentially you only return the last query, making 49 queries absolutely unnecessary
oh yes
I hadn't thought about that. How can I make a DB query that includes and excludes specific tags?
Query is:
I don't know what is the better way to get specific notes...
That line already has the right idea, you can probably merge all IncludedTags into a combined .Where filter
thanks
Just as a side note, in case this might not work, you could technically also have db queries run concurrently (Just need to take care you don't overwhelm your DB with requests) by spawning the tasks all at once and awaiting them with Task.WhenAll
I rewrote the code like this:
But now I got an error:
What can I do with the error?
cc @Sossenbinder
That means the LINQ expression you prepared can't be evaluated by EF Core (It tries to translate it into an SQL expression)
Here is a list of supported operations: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/supported-and-unsupported-linq-methods-linq-to-entities?redirectedfrom=MSDN
Supported and Unsupported LINQ Methods (LINQ to Entities) - ADO.NET
This article summarizes the standard query operators that are supported and unsupported in LINQ to Entities queries.
So you would have to restructure a bit to make sure you can prepare something on client side which EF Core understands
Oh, wait
I'm an idiot haha
The error even says what the issue is
The Split operation is not supported
how can I replace it?
EF Core tries to evaluate the whole .Where to an SQL operation, but can't deal with Split
You can probably execute the split on your client before you throw it into the IQueryable.Where(
but how... how can I split it up before I want to access it in 'where'? doesn't access to the entity tag list appear during the query?
Oh, I see, the split is part of your DB entity, my bad, I somehow thought this was part of your client side filters
I can have a look later how you can circumvent it, gotta hop into a meeting now sadly
thanks again
Sorry for being a bit late
But I guess regarding your issue: You might either go the route of structuring your identifiers in a better way on your sql table (So probably an additional table with respective mapping) or, you need to filter the data on your client
Both not very satisfactory, but that's what your options are
Thank you. I already did it the second way. But in the future I will make a separate table for tags for better performance.