Samantha J T Star
Samantha J T Star

Reputation: 32828

Can I include a method call inside a LINQ statement?

I have this code:

            var webWordForms = rootObject.webWordForms
                .Where(w => w.definition != null)
                .ToList();

            foreach (var webWordForm in rootObject.webWordForms)
            {   
                    processWordForm(word, webWordForm);               
            }

Is there a way that I can combine these two statements and call processWordForm from inside the first statement?

Upvotes: 1

Views: 35

Answers (2)

Colin Zabransky
Colin Zabransky

Reputation: 158

Use a ForEach extension on List<T>

For example,

rootObject.webWordForms
                .Where(w => w.definition != null)
                .ToList()
                .ForEach(webWordForm => processWordForm(word, webWordForm));

Upvotes: 4

Hari Prasad
Hari Prasad

Reputation: 16956

You could use ForEach method of List to chain your operation, but it is not different than what you are already doing.

 tObject.webWordForms
        .Where(w => w.definition != null)
        .ToList()
        .ForEach(wordForm => processWordForm(word, wordForm)); 

Upvotes: 1

Related Questions