Using a for loop is fine for something like a sum, but Linq is significantly easier to write and maintain when dealing with more complex generic functions, or chaining things together. Being able to define a Where filter implementation for an unknown iqueriable provider of an unknown underlying set type and call it with ~4 lines of code is a godsend, and being able to use Linq to chain together function calls for generic sets based on parameters is significantly more readable than writing a 500 line block of "if" statements.
When you're dealing with Linq you're talking about a lot more than just the syntax. It's an access system for dealing with IQueriables, IEnumerables, ICollections, etc, and interacting with those in an intuitive way using expression trees/delegates/etc.
If you don't understand the benefit it's likely just because you haven't written code yet that would see a benefit to using it.
Do you have a real world example we could discuss? That might be easier to talk meaningfully about instead of hypotheticals. I sums because it has examples directly in OP.
What sort of problems are you solving where you need to query unknown shit in unknown ways? Do you think you might have overgeneralized and overcomplicated the problem in some way?
But does it make sense to combine enumerables and queryables? Is accessing a list the same thing as accessing sets in a database? I'd say no.
One advantage is that you abstract the manner in how the data is filtered and transformed. This is useful when dealing with multiple sources of the same type of data. For example, in blended levels of cached data (local RAM, memcached/redis, and a backing database), the extension methods of LINQ can be materialized as execution plans for the remote data sources which optimizes access. For example, the database query can be created from the expression tree so you don’t have to write custom queries for every database call, nor do you need to read in useless data. You can go from three different iteration algorithms for each level of the cache to one expression that can be used optimally for each source.
But those things are not the same. Filtering a list in memory doesn't have the same failure mode as a remote db call, they don't have the same performance. They just are not equivalent. There's plenty of cases where a Linq query won't "materialize" into anything you actually want.
Don't get me wrong, high level tools like JOOQ can make sense, but unifying them across database and memory levels is just not a good idea.
Writing the SQL queries is way easier than trying to reason about what comes out of Linq, especially if you use the same expression for in memory and database access. Writing a few SQL queries just isn't that hard.
Clearly yes, the different sources have different performance characteristics, execution plans, etc. That’s the point of having caching in the first place. What LINQ provides is a high-level abstraction so that your code is clearer to interpret. C# does not really support higher kinded types generally, but LINQ does provide some aspect of that with respect to certain collection generics. Writing SQL by hand can be faster. Writing the query directly for the caching service can be faster. But the compiler support for LINQ, as well as a well-tuned evaluator for the expression trees (likely with its own memoization) lends to nearly if not perfectly comparable performance to these hand-tuned solutions, with the additional benefit of highly readable code.
-8
u/Beaverman May 25 '19
I don't understand linq and streams. What's wrong with the imperative for loop? How is it any easier to read than a for loop and a variable?
Linq and streams are not how the computer executes your code, it's not how you'd write the math. What's the point?