编码平台:ASP.NET C# 4.0
我有以下片段
public string PageID { get { return "20954654402"; } }
dynamic accounts = fb.Get("me/accounts");
if (accounts != null)
{
bool isFound = false;
foreach (dynamic account in accounts.data)
{
if (account.id == PageID)
{
isFound = true;
break;
}
}
if (!isFound)
{
// user not admin
}
else
{
}
}
两个问题
为什么更新:当我在 PageMethods 调用所有这些时,这是一个愚蠢的无关错误。(account.id == PageID)
错误(PageID 是一个字符串属性)- 是否有更简单、更像 C#4.0 的方法来更改
foreach
循环?
更新:
它是调用 Facebook API 的响应。一个样本将是
{
[{
"name": "Codoons",
"category": "Computers/technology",
"id": "20954694402",
"access_token": "179946368724329|-100002186424305|209546559074402|Hp6Ee-wFX9TEQ6AoEtng0D0my70"
}, {
"name": "Codtions Demo Application",
"category": "Application",
"id": "1799464329",
"access_token": "179946368724329|-100002186424305|179946368724329|5KoXNOd7K9Ygdw7AMMEjE28_fAQ"
}, {
"name": "Naen's Demo Application",
"category": "Application",
"id": "192419846",
"access_token": "179946368724329|61951d4bd5d346c6cefdd4c0.1-100002186424305|192328104139846|oS-ip8gd_1iEL9YR8khgrndIqQk"
}]
}
代码也更新了一点。
目的是获取与PageID
匹配的account.id
,并获取与该account.id关联的
access_token
感谢您的宝贵时间。
最佳答案
您可以使用 LINQ 方法替代 foreach:
if(accounts.Any(a => a.id == PageID))
{
// user not admin
}
else
{
}
至于它为什么“出错”:我们不能这么说,因为我们不知道id
是什么类型。但是如果 id
是 int
类型,这将解释错误。
关于c# - 将动态转换为 Lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5992277/