问题
I am using the Facebook C# SDK to get data. I want to use parallel.foreach
but cannot use it - instead, an error occurs:
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
So is there any way I can convert data retrieved to a list?
dynamic friends = app.Get("me/friends");
Parallel.ForEach(friends.data, friendsData =>
{
Interlocked.Increment(ref infoCount);
LoadFriends(friend, infoCount);
});
回答1:
If you can't use a lambda expression, have you tried an anonymous method?
dynamic friends = app.Get("me/friends");
Parallel.ForEach(friends.data, delegate(dynamic friendsData)
{
Interlocked.Increment(ref infoCount);
LoadFriends(friend, infoCount);
});
回答2:
I used the following code to convert the JsonArray to a Dictionary of id as a 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);
}
来源:https://stackoverflow.com/questions/5409894/convert-dynamic-type-to-list