Finding elements inside ExpandoObject Is it possible to simplify the code below?

dynamic model = GetExpandoObject(); //model type of ExpandoObject

var result = model.FirstOrDefault(x =&gt; x.Key == "node").Value;

if (result != null)
{
    result = ((ExpandoObject)result).FirstOrDefault(x =&gt; x.Key == "children");

    if (result != null)
    {
        result = ((IList&lt;object&gt;)((KeyValuePair&lt;string, object&gt;)result).Value).FirstOrDefault();

        if (result != null)
        {
            if (result != null)
            {
                result = ((ExpandoObject)result).FirstOrDefault(x =&gt; x.Key == "node").Value;

                if (result != null)
                {
                    result = ((ExpandoObject)result).FirstOrDefault(x =&gt; x.Key == "stuff");
                }
            }
        }
    }
}


Maybe it is possible to implement similar logic with Linq?  Any thoughts?
In your code, the variable result is just of type object because the compiler doesn't know what the result of FirstOrDefault is going to be.

Below I've used explicit typing using T for clarity and so that var doesn't evaluate to object

I created this extension method so that you wouldn't have to cast the object everytime you wanted to use it... this takes care of that before it returns

public static class ExtensionMethods
{
    public static T FirstOrDefault&lt;T&gt;(this ExpandoObject eo, string key)
    {
        object r = eo.FirstOrDefault(x =&gt; x.Key == key).Value;
        return (r is T) ? (T)r : default(T);
    }
}


What follows is a re-implementation of the original post's code using the above method

ExpandoObject model = GetExpandoObject();

var someExpandoObject = model.FirstOrDefault&lt;ExpandoObject&gt;("node");
if (someExpandoObject == null) return;

var kvp = someExpandoObject.FirstOrDefault&lt;KeyValuePair&lt;string, object&gt;&gt;("children");
if (kvp.Equals(default(KeyValuePair&lt;string, object&gt;))) return;

var ilo = kvp.Value as IList&lt;object&gt;;
if (ilo == null) return;

someExpandoObject = ilo.FirstOrDefault() as ExpandoObject;
if (someExpandoObject == null) return;

someExpandoObject = someExpandoObject.FirstOrDefault&lt;ExpandoObject&gt;("node");
if (someExpandoObject == null) return;

object finalResult = someExpandoObject.FirstOrDefault&lt;object&gt;("stuff");


What follows is another possible implementation using nested if statements instead of multiple return statements.

ExpandoObject model = GetExpandoObject();

ExpandoObject someExpandoObject;
KeyValuePair&lt;string, object&gt; kvp;
IList&lt;object&gt; ilo;
object finalResult;

if ((someExpandoObject = model.FirstOrDefault&lt;ExpandoObject&gt;("node")) != null)
{
    if (!(kvp = someExpandoObject.FirstOrDefault&lt;KeyValuePair&lt;string, object&gt;&gt;("children")).Equals(default(KeyValuePair&lt;string, object&gt;)))
    {
        if ((ilo = kvp.Value as IList&lt;object&gt;) != null)
        {
            if ((someExpandoObject = ilo.FirstOrDefault() as ExpandoObject) != null)
            {
                if ((someExpandoObject = someExpandoObject.FirstOrDefault&lt;ExpandoObject&gt;("node")) != null)
                    finalResult = someExpandoObject.FirstOrDefault&lt;object&gt;("stuff");
            }
        }
    }
}


Update:
Using a newer feature of C# null-propagation, we can string all these calls together. Now we no longer need to save each object down the chain (unless we want to).

Here is an implementation using null-propagation.

object finalResult = ((model.FirstOrDefault&lt;ExpandoObject&gt;("node")
    ?.FirstOrDefault&lt;KeyValuePair&lt;string, object&gt;&gt;("children").Value as IList&lt;object&gt;)
    ?.FirstOrDefault() as ExpandoObject)
    ?.FirstOrDefault&lt;object&gt;("stuff");