2

Can this method be rewritten using LINQ's query syntax?

public IEnumerable<Item> GetAllItems()
{
    return Tabs.SelectMany(tab =>
        {
            tab.Pick();
            return tab.Items;
        });
}

I cannot figure out where to place tab.Pick() method call.

1 Answer 1

4

No, query expressions in LINQ require each selection part etc to be a single expression, not multiple statements.

However, you could write a separate method:

public IEnumerable<Item> PickItems(Tab tab)
{
    tab.Pick();
    return tab.Items;
}

Then use:

var query = from tab in tabs
            from item in PickItems(tab)
            select item.Name;

(Or whatever you want to do.)

Sign up to request clarification or add additional context in comments.

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.