Question

I am using the Facebook C# SDK in a canvas app.

When running this code...

public IEnumerable<string> GetFansIds(string pageId, IEnumerable<string> userIds)
{
    if (userIds.Count() == 0)
        return new List<string>();

    var fb = new FacebookApp();
    string query = String.Format("select uid from page_fan where uid IN ( {0} ) and page_id = {1}",
        String.Join(",", userIds),
        pageId
    );
    dynamic result = fb.Fql(query);
    return result.Select((Func<dynamic, string>)(x => x.uid)).ToList();
}

I get the following Exception:

RuntimeBinderException: Cannot perform runtime binding on a null reference

The code does the following:

  • It performs a FQL query to get an JsonArray contaning JsonObject each with a uid Property (containing the uids of the users that are not fan of some fanpage.

  • The Select just converts all the dynamic objects into a List<string>

The FQL part just works correctly as i can see the results in the debugger. The problem is with the Select that I can't make it work.

How can I fix the dynamic lambda ??? (Please don't just tell me to use a foreach, which is what I am currently doing right now)

Was it helpful?

Solution

The problem is that extension methods cannot be used on dynamic objects. Cast the result of the query to a JsonArray and then you can use linq expressions on the JsonArray.

var result = (JsonArray)fb.Fql(query);
return result.Select((Func<dynamic, string>)(x => x.uid)).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top