Return empty linq expression

I have a WhereFilter property in a base class that looks like this:

public virtual Expression<Func<CustomerCustomerType, bool>> WhereFilter { get { return null; } } 

When it is overridden, I want to return something else instead of null, so I can use the predicatebuilder extension and (from LinqKit ), so I can write my code as follows:

 public override Expression<Func<CustomerCustomerType, bool>> WhereFilter { get { return base.WhereFilter.And(x => x.CustomerID == 1); } } 

But this gives an error, because WhereFilter is null (the object reference is not set to the object instance).

I am currently writing as:

 public override Expression<Func<CustomerCustomerType, bool>> WhereFilter { get { return x => x.CustomerID == 1; } } 

So, when there is another child class overriding this, the property of the base class will be lost.

Any way to resolve this? In sql, I used a mannequin, where 1 = 1 piece, can this be done similarly here in linq?

+7
c # linq
source share
4 answers

The LINQ equivalent of SQL 1=1 "thingy" is a predicate that always returns true:

 x => true 

Change your default method as follows:

 public virtual Expression<Func<CustomerCustomerType, bool>> WhereFilter { get { return x=>true; } } 
+16
source share

You can simply check if the inherited property returns null :

 public override Expression<Func<CustomerCustomerType, bool>> WhereFilter { get { var baseResult = base.WhereFilter; if (baseResult == null) { return x => x.CustomerID == 1; } else { return base.WhereFilter.And(x => x.CustomerID == 1); } } } 

IMO this will be more readable than seemingly meaningless dummy values ​​that will make you (or someone else who works with the code) think about what was there before. (And this will probably even result in less startup overhead.)

If you insist on using smaller lines, you can shorten the if - else with the ?: Operator.


EDIT: Thinking about this a little further, you might consider this a flaw that everyone who overrides this property will have to write so much code that could lead to errors. But then the controversial choice of design is to rely on overriding methods that do not contain such errors when invoking the inherited code, as with the model you proposed, overrides could simply return only their new predicate without calling And .

A cleaner design, IMO, will create the virtual property protected and return only a new predicate (or null ) to it. An open (and not virtual) property will then be responsible for obtaining any predicates of the current and any superclasses and combining them with And , if there are more than one.

+5
source share

You can do the following:

  public virtual Expression<Func<CustomerCustomerType, bool>> WhereFilter { get { return customerCustomerType => true; } } 

which is an expression that always returns true.

+2
source share

if you are in debugging you cannot use lambda expressions (EF), you can use this to trick linq, where if you use standard linq use this sentence with .Compile () at the end:

Expression> & lambda; (Expression.Constant (true), Expression.Parameter (TypeOf (T)))

0
source share

All Articles