2014-02-07 6 views
2

으로 변환합니다. IDictionary<Guid,string>IEnumerable<SelectListItem>으로 어떻게 변환 할 수 있습니까? 문자열을 SelectListItem으로 사용하고 싶습니다. Dictionary<,>는 본질적으로 정렬되지 않은 (또는 오히려, 순서는 변경 될 수 있으며에 의존해서는 안 :IDictionary <Guid,string>을 IEnumerable <SelectListItem>

답변

2

, 당신은 당신이 원하는 일을해야 같은

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

그럼 당신은 단지

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

은 그냥 유용한 위해하지 않을 수 있습니다 알고 있어야 사용할 수 있습니다). 당신이 값으로 GUID를 사용하려면

0

뭔가를 사용할 수 있습니다 LINQ를 사용

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

, 당신이 뭔가를 할 수있는,

var theSelectList = from dictItem in dict 
        select new SelectListItem() 
        { 
         Text = dictItem.Value, 
         Value = dictItem.Key.ToString() 
        }; 
관련 문제