Convert dynamic type to list
I am using Facebook SDK to get the data. I want to use parallel.foreach
but cannot use it - instead an error occurs:
It is not possible to use a lambda expression as an argument for a dynamically dispatched operation without first injecting it into the delegate or expression tree class
So is it possible to transform the data received into a list?
dynamic friends = app.Get("me/friends");
Parallel.ForEach(friends.data, friendsData =>
{
Interlocked.Increment(ref infoCount);
LoadFriends(friend, infoCount);
});
+1
source to share
2 answers
I used the following code to convert JsonArray to dictionary id as key and names as value
var friendlist = (friends.data as Facebook.JsonArray).ToDictionary(
p => (p as Facebook.JsonObject)["id"].ToString(),
p => (p as Facebook.JsonObject)["name"].ToString());
Parallel.ForEach(friendlist, friend
{
Interlocked.Increment(ref infoCount);
LoadFriends(friend, infoCount);
}
+1
source to share