r/csharp • u/gevorgter • 1d ago
EF core linq query syntax vs method.
Looking at the query vs method and can not decide which one is more acceptable.
For some un-explained reason i shudder when i see "from q in _db.Queues" in C#. But at the same time some things looks much less intuitive when written as methods.
2 queries bellow produce same result but i am not sure which way to stick with, so please vote #1 or #2. Or please correct #2 for me if there is another way of writing it.
#2 looks more complicated and i actually can not easily tell what resulting query will be. Will there be some unnecessary allocation for new {q,w} just to discard it since i only need queue object but had to pull webSite object to get isActvie into Where method.
1. await (from q in _db.Queues
join w in _db.WebSites on q.webSiteId equals w.id
where q.publicKey == queueKey && q.isActive && w.isActive
select q).FirstOrDefaultAsync();
2. await _db.Queues.Join(_db.WebSites, x => x.webSiteId, y => y.id, (q, w) => new { q, w }).
Where(x => x.q.publicKey == queueKey && x.q.isActive && x.w.isActive)
.Select(x => x.q).FirstOrDefaultAsync();
18
u/dgm9704 1d ago
My answer might be frustrating, but here goes. #2 method syntax. And if the code starts looking messy and complicated, then you’re either doing wrong things with the data, or the data is the wrong shape. Fix your logic, break the logic down into cleaner chunks, fix your tables, fix your indexes, create views that match the business logic, etc
4
u/apocolypticbosmer 1d ago
I strongly dislike query syntax, and prefer the LINQ style.
7
u/soundman32 1d ago edited 1d ago
FFS sort out your models. If you have to use explicitly use join in your query, it's already broken. EF will work out the required joins if your navigation properties match the DB schema FKs
2
u/pixxelkick 1d ago
The fluent api (latter) is idiomatic, query syntax is oldschool and backwars supported.
EXCEPT
When you have to manually do a join (rare but it happens), the query syntax is way easier to read and maintain imo.
So if I need to do joins, Ill do those at tge start of my query in query syntax, wrap it in a (....) and then swap to fluent api after
2
u/wubalubadubdub55 1d ago
Love the Query syntax.
It looks so simple and readable.
2
u/Merry-Lane 18h ago
Stupid question but why do you have to write joins manually? Can’t you just create relationships in the entities?
Your LINQ query would just end up being something like:
await _db.Queues
.Where( q=> q.publicKey == queueKey && q.IsActive)
.Select( q => new() { Queue= q, Website = q.Websites
.Where(w => w.IsActive).First() })
.FirstOrDefaultAsync();
Or it can actually be improved by going the other way, if a website has zero or one queue.
Without knowing exactly what you are doing and why, I can but approximate but I’m pretty sure you can write better queries
3
u/buzzon 1d ago
Always functional form . But also use .Include instead of join.
3
u/Agitated-Display6382 1d ago
Don't you have a navigation property between the two entities? If so, you can write:
_db.Queues
.Where(q => q.publicKey == queueKey && q.isActive)
.Where(q => q.WebSites.Any(s => s.isActive))
.FirstOrDefaultAsync()
1
u/smartcave 14h ago
Here's the reality. This isn't an opinion question. The C# spec makes quite clear that linq query syntax is simply syntactic sugar for methods with signatures matching some explicitly defined conventions. More than 90% of the people reading this comment didn't already know that. So, the necessary answer is the explicit method call syntax because it is the least ambiguous.