2014-12-09 3 views
0

나는 여러 개의 키, 값 객체를 반환 할 수있는 함수를 가지고 있습니다. 어떻게해야할지 모르겠다.C에서 동적 목록 또는 사전 반환

public static List <String> organizationType(User user) 
    { 
     List<String> data = new List<String>(); 
      foreach (UserRoles ur in user.GetUserRoles()) 
      { 
       OrganizationType ot = OrganizationType.Get(ur.organizationTypeId, "1"); 
       data.Add(ot.Name); // I would need a key here as well 
       data.Add(ur.roleTypeId); 
       data.Add(ur.organizationId); 

      } 


     return data; 
    } 

몇 가지 내가 JSON을 반환 할 수

var objs = organizationType(...); 

for (var i in objs){ 
    objs[var].Name; // something like this 
} 

처럼 내가 원하는 생각합니까? 어떻게하는지에 대한 아이디어가 있습니까? 나는 당신의 필요를 이해하면

+1

, 사전을 반환 : 당신이 뭔가를해야합니다. – poke

답변

1

이 내가 할 것 인 것이다 :

public static IEnumerable<string[]> organizationType(User user) 
{ 
    foreach (UserRoles ur in user.GetUserRoles()) 
    { 
     OrganizationType ot = OrganizationType.Get(ur.organizationTypeId, "1"); 
     string[] data = new string[] { ot.Name, ur.roleTypeId, ur.organizationId }; 
     yield return data; 
    } 
} 

그러나 같은

위의 의견에, 당신은 또한 트릭을 할 수있는 간단한 사전을 사용할 수 있습니다 말했다되고.

public static IEnumerable<string[]> GetOrganizationType(User user) 
    { 
     return from ur in user.GetUserRoles() 
       let ot = OrganizationType.Get(ur.organizationTypeId, "1") 
       select new[] {ot.Name, ur.roleTypeId, ur.organizationId}; 
    } 

또는 메소드 체인 :

1

LINQ 쿼리를 사용

public static IEnumerable<string[]> GetOrganizationType(User user) 
    { 
     return user.GetUserRoles() 
        .Select(ur => new[] 
           { 
            OrganizationType.Get(ur.organizationTypeId, "1").Name, 
            ur.roleTypeId, 
            ur.organizationId 
           }); 
    } 

을하지만 어쨌든 나는 사전을 사용하는 것이 좋습니다. 이미 스스로를 말했다

public static Dictionary<OrganizationType, UserRoles> GetOrganizationType(User user) 
    { 
     return user.GetUserRoles().ToDictionary(ur => OrganizationType.Get(ur.organizationTypeId, "1"), 
               ur => ur); 
    }