Extension Methods On Dynamic Object

I was in a situation where I need to parse JSON string into C#’s dynamic object and perform LINQ FirstOrDefault operation which returns dynamic object.

Apparently dynamic object won’t accept extension methods, so I have to cast it into object before applying custom extension method.

public static dynamic FirstOrDefault(this object source, Func<dynamic, dynamic> predicate)
{
    foreach (dynamic item in source as dynamic)
    {
        if (predicate(item)) return item;
    }

    return null;
}