Last post May 15, 2017 06:51 AM by Cathy Zou
May 12, 2017 11:48 AM|EssCee|LINK
public static Expression<Func<Customer, bool>> CustomerExpression()
Expression<Func<Customer, bool>> Exp = c => c.TypeId == 21 || c.TypeId == 32;
return PredicateExtensions.And(ActiveCustomersExpression(), Exp);
I've seen the above the code and after reading around I understand what the code is doing but i cant understand why and when to use an Expression, PredicateExtensions and what problem is resolves? Why not write a Linq to SQL query with Where clauses etc. Anyone
got any ideas?
May 12, 2017 04:29 PM|deepalgorithm|LINK
Expression is a very versatile way of writing dynamic code. In the case of Linq to SQL it's great for creating dynamic queries, whether it be for sorting or filtering the data. Also the implementation of LINQ providers is mostly done by
processing expression trees.
Why not write a Linq to SQL query with Where clauses etc
Because, you will have to write a lot more code.
Also Expression<Func<T> denotes a tree data structure for a lambda expression. And you use it if you want to treat lambda expressions as expression trees and look inside them versus executing them. For example, LINQ to SQL gets the expression
and converts it to the equivalent SQL statement, then submits it to server (rather than executing the lambda).
May 15, 2017 06:51 AM|Cathy Zou|LINK
but i cant understand why and when to use an Expression, PredicateExtensions and what problem is resolves? Why not write a Linq to SQL query with Where clauses etc. Anyone got any ideas?
LINQ queries will be easier to comprehend by Extending Expressions.
As we know, LINQ and Entity Framework are both commonly used in the .Net ecosystem, but even well-written applications can have LINQ queries that are difficult to understand. Because LINQ is so flexible, it can be written in ways that fail
to communicate the developer’s intent. Well-written LINQ should be so clear as to be self-documenting. To write clear LINQ, it helps to understand the details of a few LINQ components that improve LINQ’s readability.
You will been showing how to use a pipe, filter and rule pattern to make LINQ queries easier to comprehend in the following link: