2016-07-04 6 views
0

C#의 사전 데이터 유형에서 새로 도입되었습니다. 사전에서 모델 항목으로 값 가져 오기

public Dictionary<int, string> language { get; set; } 
    public string languageChoice { get; set; } 

그런 다음 내 컨트롤러에서 나는이있어 : 내가 JSON이 정수가 아닌 언어의 문자열을 객체주고 싶어

[Route("{id}/settings")] 
    [HttpGet] 
    public async Task<HttpResponseMessage> GetProjectSettings(Guid id) 
    { 
    var projectSettings = new ProjectSettings 
    { 
     Id = id,  
     language = new Dictionary<int, string>() { 
          {1,"English"}, 
          {2,"Spanish"}}, 
     languageChoice = //get language by ID   

    }; 

    if (projectSettings != null) 
    { 
     return Request.CreateResponse<ProjectSettings>(HttpStatusCode.OK, projectSettings); 

    } 
    else 
    { 
     return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found"); 
    } 
    } 

나는이 모델 항목을 얻었다. 어떻게해야합니까? 변경하지 않으려면

종류가

+0

그래서 {{ "1": "영어"}'를 원하십니까? –

+0

예를 들어 json 개체에서이 언어처럼 갖고 싶습니다. 선택 : "1"이고 언어 선택이 아닙니다. – Fearcoder

+0

다음과 같이 :'[{ "languageChoice": 1}, { "languageChoice": 2}] ' –

답변

0

과 관련하여 Dictionary<int, string>은 다음 그냥에서 새 Dictionary<int, int>를 만들 수 있습니다 기존. 1 원하는 언어 ID입니다

var alteredDictionary = languages.ToDictionary(x => x.Key, x => x.Key); 

그런 다음

var projectSettings = new ProjectSettings 
{ 
    Id = id,  
    language = new Dictionary<int, string>() { 
         {1,"English"}, 
         {2,"Spanish"}}, 
    languageChoice = alteredDictionary[1]// optional .ToString() if you want string   
}; 

을한다.