Invalid cast from List to IEnumerable back to List, why?

后端 未结 5 1331
无人及你
无人及你 2021-01-13 11:02

So basically i have this method.

public List FilterCustomersByStatus(List source, string status)
{
    return (List

        
5条回答
  •  清歌不尽
    2021-01-13 11:36

    As others pointed out, you need to use ToList to convert the result to List.

    The reason is that Where is lazily evaluated, so Where does not really filter the data. What it does is create an IEnumerable which filters data as needed.

    Lazy evaluation has several benefits. It might be faster, it allows using Where with infinite IEnumerables, etc.

    ToList forces the result to be converted to List, which seems to be what you want.

提交回复
热议问题