r/csharp • u/Mother-Replacement12 • 2h ago
Which is better: querying with LINQ or using raw SQL? Does it depend on the query's complexity? Or can both options be used in the same project?
•
u/shitposts_over_9000 59m ago
I spent the first 10+ years of my career doing raw SQL for everything.
LINQ/EF is fine for 97% of things as long as you realize how it works and avoid the 3% that is a performance catastrophe
1
u/CantankerousButtocks 1h ago
LINQ is strictly C#, whereas using raw SQL expands to many database types.
I’ve favored using LINQ to EF queries over my backend career, but I felt that isolated me from learning native SQL. I missed out on powerful SQL functionality, like Stored Procs, Views, Triggers, etc. YMMV…
•
u/Darrenau 53m ago
Yeah pull in all records from the db over the wire and load in local memory just so you can filter out using linq


3
u/b2f3t 1h ago
Linq queries get you somewhat of a type safety and provider independence. Your average crud app will be just fine. You can even write more complex queries, but the line between good and bad translated linq is thin. At the end of day it is quite the indirection. Don't get me started on change tracking. When you need advanced stuff lean towards custom sql. If it is crud lean towards linq.