2 ms·
Public Service Announcement: LINQ is not just syntactic sugar for processing collections. The beauty and power of Linq is that it is translated to expression t
by scarface74 7y ago
Public Service Announcement: LINQ is not just syntactic sugar for processing collections.
The beauty and power of Linq is that it is translated to expression trees at runtime, can be interpreted by a provider, transformed to basically anything - sql, MongoQuery, intermediate language, etc. - and you can pass around types of:
Expression<Func<T,bool>>
to different providers.
Like
repo.Find<Customer>(c => c.Age > 50 && c.Sex == “M”);
- jeswin 7y ago> The beauty and power of Linq is that it is translated to expression trees at runtime It's compiled into Expression Trees at compile-time (if it's an IQueryable). If it's an IEnumerable, Expression Trees are never created in the first place (you get a function).
- scarface74 7y agoThere is a reason I wrapped everything in Expression<> to avoid the mistake I made way too often when sending a Func<T,bool> to an EF context....
- jeswin 7y ago> There is a reason I wrapped everything in Expression<> to avoid the mistake I made way too often when sending a Func<T,bool> to an EF context. Sorry, I did not follow.
- scarface74 7y agoNothing will stop you from passing in a Func<T,bool> type where you should be using an expression, but if you pass it to a Where on an EF context it will be run on the client instead of being converted to sql.
- jeswin 7y agoI was saying that Expression Trees are created at "compile-time", and not at runtime. Here's the CIL that gets generated in the case of IQueryable - https://stackoverflow.com/questions/3894598/how-is-linq-compiled-into-the-cil https://stackoverflow.com/questions/3894598/how-is-linq-comp...
- nightski 7y agoThere is an entire API for building up dynamic expression trees and compiling them at runtime.
- mythz 7y ago...and zero for the inverse, which is why Expression Trees are generated by the C# compiler at compile time, not decompiled at runtime.
- nightski 7y agoYou are right (as far as built in support goes), but it actually could be a fun project to take IL and build up C# expression trees from it. Especially given there are plenty of IL to C# de-compilation libraries out there.
- nizmow 7y agoIn EF Core 3.x it'll now throw an exception at runtime.
- mythz 7y agoIt's a compile time, not a runtime feature. It can't be implemented at runtime, if your method is passed an opaque compiled delegate, it's not possible for the .NET Runtime to deconstruct its pre-compiled AST. If your method accepts a `Expression<Func<T,bool>>` the C# compiler passes it the AST of the Expression instead of the compiled delegate. For an example, paste the code below to https://sharplab.io https://sharplab.io using System; public class A { A() => M<int>(x => x == 1); public void M<T>(Func<T,bool> expr) {} } public class B { B() => M<int>(x => x == 1); public void M<T>(System.Linq.Expressions.Expression<Func<T,bool>> expr) {} } and look at what the compiler generates for class A vs class B's constructors.
- kjlkjljklkjl 7y agobut, Expression can be created from string, doesn't it?
- mythz 7y agoParsed by who, the C# compiler? I'm not aware of anything that generates C#'s Expression AST from a string, other than a C# Compiler. You can use Roslyn in code, but that's using the C# compiler in process.
- gpderetta 7y agoThe code->AST is done of course at compile time (there is no reason to delay), but I believe the expression tree itself is processed by linq at runtime. You can build expressions programmatically at runtime as well (and then ask the runtime to optmize them to bytecode). I remember doing it years ago, it was surprisingly simple (and very powerful).
- mythz 7y agoOf course the Expression's AST generated by the C# compiler is analyzed at runtime, how else could it be done?