0

Can some one help me? I'm not very strong with lambda expressions.

protected void Process1(List<SomeClass> mylist)
{
    foreach (var item in mylist)
    {
        if (!SomeClass.Validate(item)) 
        { 
            continue; 
        } 
        DoStuff(item); 
        DoMore(item); 
        DoEven(item);
    }
}

protected void Process2(List<SomeClass> mylist)
{
    foreach (var item in mylist)
    { 
        if (!SomeClass.Validate(item) || item.Value == 0)
        {
            continue;
        }
        DoStuff(item);
        DoMore(item);
        DoEven(item); 
    }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
CyberDx
  • 29
  • 2

6 Answers6

3

Second process is the same as calling first process with parameter

myList.Where(item => item.Value != 0)

Or, if you need both methods:

protected void Process1(IEnumerable<SomeClass> mylist)
{
    foreach (var item in mylist)
    { 
        if (!SomeClass.Validate(item))        
            continue;

        DoStuff(item);
        DoMore(item);
        DoEven(item); 
    }
}

protected void Process2(IEnumerable<SomeClass> mylist)
{
    Process1(myList.Where(item => item.Value != 0));
}

Also, I'd changed input parameter to IEnumerable (because you only iterating through sequence of items).

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3
protected void Process1(List<SomeClass> mylist)
{
    foreach (var item in mylist.Where(item => SomeClass.Validate(item))
    {
        DoStuff(item); 
        DoMore(item); 
        DoEven(item);
    }
}

protected void Process2(List<SomeClass> mylist)
{
    foreach (var item in mylist.Where(item => item.Value != 0 && SomeClass.Validate(item))
    { 
        DoStuff(item);
        DoMore(item);
        DoEven(item); 
    }
}

That's how I would get it done.

BGregorius
  • 51
  • 4
1

You mean something like this?

protected void Process(List<SomeClass> mylist, List<Action<SomeClass>> actions)
{
    foreach (var item in mylist)
    {
        if (!SomeClass.Validate(item)) 
        { 
            continue; 
        }
        foreach(var action in actions) 
            action(item); 
    }
}

then call it with:

Process(list, new List<Action<SomeClass>> {DoStuff, DoMore, DoEven});
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

One thing you can do is to use Linq to apply the validation to the collection and just return valid items. Additionally, you can chain them together to apply multiple validations, if needed. For instance:

return myList.Where(item -> !SomeClass.Validate(item))
             .Where(item -> !OtherClass.Validate(item))

This would return a collection of just the items that pass validation, to which you could apply whatever "stuff" you need to "do".

jtheis
  • 916
  • 3
  • 12
  • 28
0

The Process method can be made very generic and reusable:

void Process<T>(IEnumerable<T> source, Predicate<T> filter, Action<T> action)
{
    foreach (var item in source)
        if (filter(item))
            action(item);
}

Then you will use it, for example, like this:

Process(
    list, 
    i => SomeClass.Validate(i) && i.Value != 0, 
    i => 
    {
        DoStuff(i);
        DoMore(i);
        DoEven(i);
    });
Wasp
  • 3,395
  • 19
  • 37
-1

I'm not a big fan of passing lists around to begin with, but...

protected void Process1(List<SomeClass> myList)
{
    myList.Where(item => SomeClass.Validate(item)).ToList()
        .ForEach(item =>
        {
            DoStuff(item);
            DoMore(item);
            DoEven(item);
        });
}

protected void Process2(List<SomeClass> myList)
{
    myList.Where(item => SomeClass.Validate(item) && item.Value != 0).ToList()
        .ForEach(item =>
        {
            DoStuff(item);
            DoMore(item);
            DoEven(item);
        });
}
Cole Cameron
  • 2,213
  • 1
  • 13
  • 12
  • 2
    -1 IEnumerable has no extension method `ForEach` – L.B Oct 24 '12 at 20:48
  • You are correct, sir. Forgot the ToList(). – Cole Cameron Oct 24 '12 at 20:57
  • @L.B - It is List not just an IEnumerable and it has ForEach extension method. Please remove your down votes. – Tariqulazam Oct 24 '12 at 20:59
  • Now do you think it is better then a simple `if`? – L.B Oct 24 '12 at 20:59
  • @Tariqulazam When you apply `where` to `List`, you don't have `ForEach` anymore. Try to compile `new List().Where(x=>x>0).ForEach(i => { int x = 1; });` before commenting – L.B Oct 24 '12 at 21:01
  • @L.B - Yes, that's true. I was only thinking about the parameters passed to the functions. You should have provide better clarification with each down vote. However, thanks for pointing that out. – Tariqulazam Oct 24 '12 at 21:06
  • "I'm not a big fan of passing lists around to begin with, but..." Could you clarify why not? – lesderid Oct 24 '12 at 21:36