r/csharp • • 14h 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();
2 Upvotes

16 comments sorted by

13

u/dgm9704 13h 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

1

u/gevorgter 13h ago

I am actually with you, my only frustration is with the (q,w)=>new {q,w}

Although it does not result in un-necessary allocation of new object it still rubs me the wrong way. But sql syntax in my C# code irks me more.

4

u/qrzychu69 12h ago

You know that there is no object involved, right?

IQueryable is translated to SQL, and it's all executed in the database

That's the cool magic bit that makes it amazing

0

u/binarycow 10h ago

Unless you're using those methods on IEnumerable.

3

u/Agitated-Display6382 12h 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()

5

u/soundman32 13h ago edited 12h 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

u/Frequent_Field_6894 12h ago

Mr boyce and codd turning in their graves….

0

u/[deleted] 12h ago

[deleted]

5

u/Disastrous_Fill_5566 12h 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

u/[deleted] 12h ago

[deleted]

2

u/Disastrous_Fill_5566 11h 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.

5

u/soundman32 12h 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. 

-2

u/WetSound 12h ago

It sounds like you never wrote real code

2

u/Kant8 10h ago

It sounds like you never properly configured your entities or db in general

1

u/soundman32 12h 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

u/WetSound 12h ago edited 12h ago

That doesn't mean it's good or solves complex problems

2

u/apocolypticbosmer 8h ago

I strongly dislike query syntax, and prefer the LINQ style.

3

u/buzzon 13h ago

Always functional form . But also use .Include instead of join.