Is there a way to translate predicate(Func<T,bool>) using LINQ to SQL query or something similar to this?
Found nothing on internet or MS guides.
CodePudding user response:
Short awnser : no Long awnser : yes... but actually no
There is no way to accuratly translate a programming language into another without the risk of the function and meaning being altered (you could use this but even that is no longer mantained)
I would recommend you learning LinQ in order to safely translate it without mistakes. You could also post you query so anyone like me or other more fitted members to help you out in the translation.
CodePudding user response:
ORMs (like Entity Framework, linq2db, and others) in C# usually use expression trees and IQueryable
to be able to translate the code into actual SQL queries, so they need Expression<Func<T, bool>>
, not just Func<T, bool>
. If you are creating the predicate via lambda then compiler can also translate it to expression tree, i.e.:
Func<MyClass, bool> predicate = mc => mc.IntProp == 1;
Exppression<Func<MyClass, bool>> predicate = mc => mc.IntProp == 1;
Will work both well. So in some cases you can just change your method parameter and that's it.
Read more:
- Expression Trees
- What is the difference between IQueryable and IEnumerable?
- LINQKit - interesting library which does some magic around LINQ