如何转换List <dictionary <string,object =“”>&gt;到每个项目的列表<string>&#34;&#34; name&#34; lambda中的财产?

时间:2017-05-10 19:44:30

标签: c# dictionary lambda

我有一个List<Dictionary<string, object>>,其价值如下:

{ name: "Joe", age: 23, ... },
{ name: "Jack", age: 19, ... },
...

我想从保存名称的List<string>创建一个name

我该怎么做(和演员表一起)?

我知道如何按名称选择值,但是如何使用lambda返回List<string> names = objects.Select( item => item[ "name" ].ToString() ).ToList<string>(); 字段并将其转换为列表的字符串?

这是对的吗?

    // push view controller but animate modally
    let storyBoard: UIStoryboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "myViewControllerIdentifier") as! MyViewController

    let navigationController = self.navigationController


    vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: vc, action: #selector(vc.closeView))
    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: vc, action: nil)

    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionMoveIn
    transition.subtype = kCATransitionFromTop
    navigationController?.view.layer.add(transition, forKey: nil)
    navigationController?.pushViewController(vc, animated: false)

1 个答案:

答案 0 :(得分:5)

看看是否有效:

var names = objects.Select(item => (string)item["name"]).ToList();

但是,如果我可以提出建议,你就不应该这样使用字典。考虑为你的词典中的数据创建一个类。

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    // etc
}