2014-02-07 70 views

回答

2

如果你想使用GUID作为值,你可以使用

dictionary.Select(x => new SelectListItem { Text = x.Value, Value = x.Key }) 
2

那么你可以使用

dictionary.Values().Select(x => new SelectedListItem { Text = x }) 

要知道,它可能不是一个有用的顺序:Dictionary<,>本质上是无序的(或者更确切地说,该命令可能会改变,不应该依赖)。

0

像这样的东西应该做你想要什么:

var selectList = dictionary 
    .OrderBy(kvp => kvp.Value) // Order the Select List by the dictionary value 
    .Select(kvp => new SelectListItem 
    { 
     Selected = kvp.Key == model.SelectedGuid, // optional but would allow you to maintain the selection when re-displaying the view 
     Text = kvp.Value, 
     Value = kvp.Key 
    }) 
    .ToList(); 
0

使用LINQ,你可以做这样的事情,

var theSelectList = from dictItem in dict 
        select new SelectListItem() 
        { 
         Text = dictItem.Value, 
         Value = dictItem.Key.ToString() 
        }; 
相关问题