0

is there a way for bellow lambda expression to move the method call ConvertFilterType(filter.FilterTypeId) into a variable so it is not called repeatedly for every condition ?

This if statement is making sure collection consist of all required filters.

if (run.Filters.All(
           filter => (ConvertFilterType(filter.FilterTypeId) != FilterType.A)
                  && (ConvertFilterType(filter.FilterTypeId) != FilterType.B)
                  && (ConvertFilterType(filter.FilterTypeId) != FilterType.C)
                  && (ConvertFilterType(filter.FilterTypeId) != FilterType.D)
                  && (ConvertFilterType(filter.FilterTypeId) != FilterType.E)))
    {
       throw new ArgumentException();
    } 
1
  • 1
    you can use { } in a lambda expression Commented Aug 14, 2013 at 11:45

5 Answers 5

6
if (run.Filters.All(
       filter => {
              FilterType t = ConvertFilterType(filter.FilterTypeId);
              return
              t != FilterType.A && t != FilterType.B && t != FilterType.C && t != FilterType.D && t != FilterType.E;                        
         }))
   {
    throw new ArgumentException();
   } 
Sign up to request clarification or add additional context in comments.

Comments

3

King beat me to it. Although it is not what you asked, I would also recommend that for improved readability, that you create a separate method with a more meaningful name. This chain of logic can obscure the intent of what you are doing. Extracting it into a method would help clarify the method's intent. I would have put this in the comment's to King's post, but don't have the reputation yet.

Here is some sample code, though I don't know what the type of the items in your Filters collection to know what the parameter of the method would need to be.

   if (run.Filters.All(filter => { return IsFilterAllowed(filter); } )
   {
       throw new ArgumentException();
   }

    private bool IsFilterAllowed(FiltersItemType filter)
    {
        FilterType t = ConvertFilterType(filter.FilterTypeId);
        return
              t != FilterType.A &&
              t != FilterType.B &&
              t != FilterType.C &&
              t != FilterType.D &&
              t != FilterType.E;
    }

2 Comments

This is a very good point ! Thx. I am sure you will gain your reputation soon.
I just don't spend enough time on here contributing.
2

King's answer is the way to go but could you could also do this it seems.

FilterType[] notTheseFilters = new FilterType[] { FilterType.A, FilterType.B...};
bool result = !Filters.Any(f =>
                    notTheseFilters.Contains(ConvertFilterType(f.FilterTypeId)));

Comments

0

If my logic serves me right, you could also do this

var filterTypesToAvoid = new[]{
     FilterType.A,
     FilterType.B,
     FilterType.C,
     FilterType.D,
     FilterType.E
};

if (run.Filters.All(
        filter => !filterTypesToAvoid.Contains(ConvertFilterType(filter.FilterTypeId))
    ))
    {
       throw new ArgumentException();
    } 

This way there is no need for brackets in the LINQ query.

Comments

0

To extend what brader24 proposed, you could try :

 if (run.Filters.All(IsFilterAllowed))
 {
   throw new ArgumentException();
 }

private bool IsFilterAllowed(FiltersItemType filter)
{
    FilterType t = ConvertFilterType(filter.FilterTypeId);
    return
          t != FilterType.A &&
          t != FilterType.B &&
          t != FilterType.C &&
          t != FilterType.D &&
          t != FilterType.E;
}

Comments

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.