Unable to step into or break in method called inside Linq query/expression
Asked Answered
D

1

6

I have encountered a bizarre issue trying to step into a method called from within a Linq query (although I also experience the problem when using a Linq expression). The code compiles, AND it appears to work (I get the outcome I was expecting).

IEnumerable<TagBuilder> theOutcome = from r in resultSet
                                     select GetTableDataRow(null);

OR

IEnumerable<TagBuilder> theOutcome = resultSet.Select(r => GetTableDataRow(null));

The method being called is defined as follows:

private static TagBuilder GetTableDataRow(IEnumerable<object> rowData)
{
    TagBuilder tr = new TagBuilder("tr");
    return tr;
}

The resultSet variable is an IPagedList that contains two items.

The variable theOutcome ends up holding two TagBuilder instances as expected.

However, I cannot then step into GetTableDataRow(), even if I put a breakpoint on or before the Linq query in question. If I put a breakpoint IN the GetTableDataRow() method, it never hits that either.

I'm completely stumped. Can anyone help? Code is obviously very simple right now, but I'll NEED to step through the contents of that method with the debugger as I develop it.

Diffractometer answered 10/4, 2015 at 7:59 Comment(0)
L
17

You need to evaluate the expression.

Just call ToArray(), ToList(), Count(), or any other method or extension on IEnumerable<T> which forces evaluation.

The result of Select is evaluated using deferred execution, so nothing happens with GetTableDataRow until the query is used.


More explicitly, you can see this by expanding out what Select does:

IEnumerable<TagBuilder> theOutcome = resultSet.Select(r => GetTableDataRow(null));

is equivalent to

IEnumerable<TagBuilder> theOutcome = getRows(resultSet);

where getRows is:

IEnumerable<TagBuilder> getRows(IEnumerable<IPagedList> source)
{
    foreach ( IPagedListitem in source )
        yield return GetTableDataRow(null);
}

Because GetTableDataRow is yield returned, it's not evaluated until the evaluation is forced (e.g. by ToArray(), etc.).

Linwoodlinz answered 10/4, 2015 at 8:2 Comment(3)
Putting .ToArray() onto the end of the Select does allow me to step into the method! I had a look at the article you linked but I don't really understand the relationship of lazy evaluation to this issue, in the sense that isn't the Linq being evaluated because I'm getting output into theOutcome variable? It looks like you've edited it out now so perhaps it was not relevant after all. I would be interested in knowing what's going on though.Diffractometer
I edited it out because it wasn't a relevant link... I've edited my answer to a better link.Linwoodlinz
When you say The variable theOutcome ends up holding two TagBuilder instances as expected in your question, the debugger is evaluating the enumerable...Linwoodlinz

© 2022 - 2024 — McMap. All rights reserved.