Is there a proper way to query ExpandoObject? I am receiving the JSON from WebService and populating ExpandoObject from this one:

 var converter = new ExpandoObjectConverter();
 var jsonObject = JsonConvert.DeserializeObject&lt;ExpandoObject&gt;(json, converter);


The JSON object is quite complex and in order to find the value I am doing this spaghetti code:

KeyValuePair&lt;string, object&gt; CountPair = new KeyValuePair&lt;string, object&gt;();

var currentCountPair = jsonObject.FirstOrDefault(x =&gt; x.Key == "data").Value;

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

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

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

            if (currentCountPair != null)
            {
                CountPair = ((ExpandoObject)currentCountPair).FirstOrDefault(x =&gt; x.Key == "score");
                result.Score = CountPair.Value == null ? null : CountPair.Value.ToString();
            }
        }
    }

}


What do you think?  Is there any better way of getting that value from ExpandoObject?
Or at least to reduce the number of if statements and code complexity...
First, I don't see any reason for using ExpandoObject in your code. The same result could be achieved with cleaner code using JObjects.

Second, what would help if you could express your query using “LINQ to null”. Basically, treat null as an empty collection and any other value as a collection containing just that value. (The technical term for this is the Maybe monad.)

With both of these changes, your code could look something like this:

var jsonObject = JsonConvert.DeserializeObject&lt;JObject&gt;(json);

string result =
    from o in jsonObject.ToNullWrapper()
    from data in o["data"]
    from children in data["children"]
    from child in children.FirstOrDefault()
    from data2 in child["data"]
    from score in data2["score"]
    select score.ToString();


Note that (ab)using LINQ like this is not idiomatic and probably very confusing. But I think it results in very clean code.

To make this work, you need the implementation of LINQ to null:

public struct NullWrapper&lt;T&gt;
    where T : class
{
    public T Value { get; private set; }

    public NullWrapper(T value)
        : this()
    {
        Value = value;
    }

    public static implicit operator T(NullWrapper&lt;T&gt; wrapper)
    {
        return wrapper.Value;
    }
}

public static class NullExtensions
{
    public static NullWrapper&lt;T&gt; ToNullWrapper&lt;T&gt;(this T value)
        where T : class
    {
        return new NullWrapper&lt;T&gt;(value);
    }

    public static NullWrapper&lt;TResult&gt;
        SelectMany&lt;TSource, TIntermediary, TResult&gt;(
        this NullWrapper&lt;TSource&gt; source,
        Func&lt;TSource, TIntermediary&gt; intermediarySelector,
        Func&lt;TSource, TIntermediary, TResult&gt; resultSelector)
        where TSource : class
        where TIntermediary : class
        where TResult : class
    {
        if (source.Value == null)
            return new NullWrapper&lt;TResult&gt;();

        var intermediary = intermediarySelector(source.Value);

        if (intermediary == null)
            return new NullWrapper&lt;TResult&gt;();

        return resultSelector(source.Value, intermediary).ToNullWrapper();
    }
}