简体   繁体   English

将动态类型转换为列表

[英]Convert Dynamic Type to List

I am using the Facebook C# SDK to get data. 我正在使用Facebook C#SDK来获取数据。 I want to use parallel.foreach but cannot use it - instead, an error occurs: 我想使用parallel.foreach但不能使用它 - 相反,会发生错误:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type 如果不首先将lambda表达式转换为委托或表达式树类型,则不能将lambda表达式用作动态分派的操作的参数

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);
    });

If you can't use a lambda expression, have you tried an anonymous method? 如果你不能使用lambda表达式,你尝试过匿名方法吗?

dynamic friends = app.Get("me/friends");   
    Parallel.ForEach(friends.data, delegate(dynamic friendsData)
       {
           Interlocked.Increment(ref infoCount);
           LoadFriends(friend, infoCount);

       });

I used the following code to convert the JsonArray to a Dictionary of id as a key and names as value 我使用以下代码将JsonArray转换为id作为键的Dictionary,并将其命名为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);
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM