Convert Dynamic Type to List

懵懂的女人 提交于 2019-12-11 07:04:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!