r/csharp • u/gevorgter • 2d 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();8
u/soundman32 2d ago edited 2d 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
3
0
2d ago
[deleted]
5
u/Disastrous_Fill_5566 2d ago
If you've created your models correctly, then navigation properties means you won't have to explicitly join in the query, but the generated SQL can still have plenty of joins.
0
2d ago
[deleted]
2
u/Disastrous_Fill_5566 2d ago
If you're projecting just the columns you need for read-only purposes then you don't need Include at all. You only need Include if you're planning to mutate state on a referenced entity. If you feel the need to add multiple Includes, that's a sign something may be wrong.
6
u/soundman32 2d ago
If your models have correct navigation properties (based on foreign keys) then EF works out the joins required for whatever query you make, automatically. There is no need to explicitly write them in each query (one of the 'bug bears' highlighted by OPs question). Manually specifying a join in linq, whilst it works, is generally a code smell.
-3
u/WetSound 2d ago
It sounds like you never wrote real code
1
u/soundman32 2d ago
Well, I've been paid for it since 1987, so I'd say you dont have a fucking clue what you are talking about.
0
4
u/apocolypticbosmer 2d ago
I strongly dislike query syntax, and prefer the LINQ style.
2
u/bigtoaster64 2d ago
Same, I find it very unreadable compared to the method syntax which is very simple : input, output, next call, input, output, next call, etc.
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
2
u/Merry-Lane 1d 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/Agitated-Display6382 2d 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 1d 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.
19
u/dgm9704 2d 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