1

I want to apply a condition, after I passed another condition like this:

List<Entity> GetByCondition()
{
  If(dateConditionEnabled)
    {
      Repository.Get(i=>i.start<=datetime.Now && i.EndDate<=DateTime.Now);
    }
  else
    {
        Repository.Get();
    }
}  

I want to apply "If" condition, in the lambda I used in Get method.

Any idea?

-Regards

2 Answers 2

3

Is Get() equivalent to Get( i => true)? In this case you can use:

List<Entity> GetByCondition()
{
      return Repository.Get(!dateConditionEnabled || i=>i.start<=datetime.Now && i.EndDate<=DateTime.Now);
}  
Sign up to request clarification or add additional context in comments.

Comments

2

Why not

List<Entity> GetByCondition()
{
      return Repository.Get(i=>!i.dateConditionEnabled || i.start<=datetime.Now && i.EndDate<=DateTime.Now);
} 

?

3 Comments

Thank you Hamlet, Henrik answered first. but the first part, !i.dateConditionEnabled of your post is correct.
Is 'dateConditionEnabled' a member of the repository entry or is it a part of the class which contains GetByCondition()? If a part of the class then i => !i.dateConditionEnabled will give an error.
dateConditionEnabled is a property of my entity, but no, there is no error here :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.