Forum Discussion

elderfd's avatar
elderfd
Frequent Visitor
8 years ago
Solved

List.Select does not work as expected when List contains Errors

When a List contains Errors, List.Select does not work as expected. First we generate a list with an Error,

List = {"Hello", 2, #date("not a date")}

The third element of this list is an Error.

 

If we apply List.Select with a condition that no elements can fulfil,

SelectedWithFalse = List.Select(List, each false)

we would expect to get an empty list, but instead get a List containing only the final Error value.

 

If we apply List.Select with a condition that all elements fulfil,

SelectedWithTrue = List.Select(List, each true)

we would expect to get the original list again, but instead get a one element list which contains the first value ("Hello") and which is now marked as an Error. However there is no message attached to this Error, if selected it just shows the original value ("Hello").

  • The reason for this behavior is that lists in M are lazy. The previews (in the Query Editor) of both the "each false" and "each true" queries actually produce the same result. In both cases, the Query Editor attempts to show all the values, which causes List.Select to traverse its entire source list. In the process, it encounters the error value, which interrupts its enumeration of the source list, resulting in a list that contains an error.

     

    However, when you navigate into the first item in the resulting list, both the "each false" and "each true" queries attempt to get only the first item resulting from the List.Select. Since the "each true" query's List.Select condition will "match" any input, the “Hello” value is selected and returned, and the error value is never encountered. However, in the "each false" query, the first two inputs do not "match", and thus it moves on to the error value, which interrupts its enumeration and causes the error to bubble up.

     

    Hope this helps clarify things.

    Ehren

5 Replies

  • Ehren's avatar
    Ehren
    Microsoft Employee

    The reason for this behavior is that lists in M are lazy. The previews (in the Query Editor) of both the "each false" and "each true" queries actually produce the same result. In both cases, the Query Editor attempts to show all the values, which causes List.Select to traverse its entire source list. In the process, it encounters the error value, which interrupts its enumeration of the source list, resulting in a list that contains an error.

     

    However, when you navigate into the first item in the resulting list, both the "each false" and "each true" queries attempt to get only the first item resulting from the List.Select. Since the "each true" query's List.Select condition will "match" any input, the “Hello” value is selected and returned, and the error value is never encountered. However, in the "each false" query, the first two inputs do not "match", and thus it moves on to the error value, which interrupts its enumeration and causes the error to bubble up.

     

    Hope this helps clarify things.

    Ehren

    • elderfd's avatar
      elderfd
      Frequent Visitor

      Thanks Ehren for the explanation.

       

      However, I am still slightly confused. If the Error ceases the traversal of the List, would we not expect the "each true" query to return the first two non-Error elements of the list rather than only the first?

      • Ehren's avatar
        Ehren
        Microsoft Employee

        Good question. When you navigate into the first element in the "each true" query, try replacing the generated list index of {0} with {1}, and you should see the second value in the list.

         

        The error doesn't just cause the enumeration of the list to stop...it actually changes the result of enumeration to an error.

        Ehren

  • v-qiuyu-msft's avatar
    v-qiuyu-msft
    Community Support

    Hi elderfd,

     

    I have sent a email to consult this issue internally, will update here once I get any information. 

     

    Best Regards,
    Qiuyun Yu