0

I have the following code:

foreach (Question question in upd)
{
    if (question.AssignedTo == null)
    {
        question.QuestionStatusId = 6;
    }
    else
    {
        question.AssignedDate = DateTime.UtcNow;
        question.QuestionStatusId = 5;
    }
    _uow.Questions.Update(question);
}

Where upd is a List

It works but I am wondering if there is a simpler way for me to code this. Before I was using the following:

upd.ForEach(_obj => _uow.Questions.Update(_obj));

However now I cannot do like this as I have some code that must run before each update.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 1
    You could probably write a function which does it for you, then use that. `upd.ForEach(_obj => TheFunctionToHandleUpdates(_obj));` – Tobberoth Dec 13 '13 at 13:24
  • You can check this answer : http://stackoverflow.com/questions/200574/linq-equivalent-of-foreach-for-ienumerablet hope this helps ;] – dlght Dec 13 '13 at 13:25
  • You might want to convert those hard-coded numbers in to an enum! – Moo-Juice Dec 13 '13 at 13:28
  • 3
    What's wrong with the `foreach` loop? – sloth Dec 13 '13 at 13:30
  • A lambda expression can either be a single-line statement, without the `return` keyword, or a multiline statement, wrapped in `{ }` and requiring an explicit `return`. Use the latter if you have to put checks or other code in before the update call. – Graham Dec 13 '13 at 13:36

1 Answers1

3

You can put any block of code in a Linq extension that takes an Action. Assuming ForEach is defined as:

public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
    foreach(T item in enumeration)
    {
        action(item);
    }
}

Then

upd.ForEach(_obj => 
 {
    if (question.AssignedTo == null)
    {
        question.QuestionStatusId = 6;
    }
    else
    {
        question.AssignedDate = DateTime.UtcNow;
        question.QuestionStatusId = 5;
    }
    _uow.Questions.Update(_obj);
  }
);
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
  • +1, but in its current state this answer merely uses an action and obfuscates the for each call (not saying `ForEach` is bad!), perhaps it would clearer if you defined a `void UpdateMethod(Question question)` that did the code and wrap *that* up in the `ForEach` call. – Moo-Juice Dec 13 '13 at 13:31
  • @Moo-Juice which part obfuscates the foreach ? The method is _called_ `ForEach`... – Gusdor Dec 13 '13 at 13:32
  • Moo-Juice agreed, having that doesn't make a lot of sense but was what was asked and I just wanted to clarify you can put a block of code inside a LinQ expression, I don't see how that's useful or more readable either. – Jorge Córdoba Dec 13 '13 at 13:33
  • @Gusdor, in its current form - the answer merely delegates the `foreach` call to an extension method `ForEach`, and the clarity isn't necessarily improved. By introducing the function itself - we will gain more readability in this approach. – Moo-Juice Dec 13 '13 at 13:40