2012-04-11 4 views
2

내 방법으로 첫 번째 예 http://www.dotnetperls.com/convert-list-string을 구현하기 위해 노력하고있어하지만 난 방법을하기위한 곳 중 2 번째 인수와 일치하는 힘든 시간을 보내고 있습니다 string.Join하려고 :IList의

string printitout = string.Join(",", test.ToArray<Location>); 

오류 메시지 :

The best overloaded method match for 'string.Join(string, 
System.Collections.Generic.IEnumerable<string>)' has some invalid arguments 

모든 IList 인터페이스는 IEnurmerable로 구현됩니다 (누군가가 원한다면 여기에 나열되지 않음).

class IList2 
{ 
    static void Main(string[] args) 
    { 

    string sSite = "test"; 
    string sSite1 = "test"; 
    string sSite2 = "test"; 

    Locations test = new Locations(); 
    Location loc = new Location(); 
    test.Add(sSite) 
    test.Add(sSite1) 
    test.Add(sSite2) 
    string printitout = string.Join(",", test.ToArray<Location>); //having issues calling what it needs. 

    } 
} 
string printitout = string.Join(",", test.ToArray<Location>); 


public class Location 
{ 
    public Location() 
    { 

    } 
    private string _site = string.Empty; 
    public string Site 
    { 
     get { return _site; } 
     set { _site = value; } 
    } 
} 

public class Locations : IList<Location> 
{ 
    List<Location> _locs = new List<Location>(); 

    public Locations() { } 

    public void Add(string sSite) 
    { 
     Location loc = new Location(); 
     loc.Site = sSite; 
     _locs.Add(loc); 
    } 
} 

편집 : 확인 "string.Join ("사용 ", 테스트);" 대신 목록에 무엇이 어떤 이유로

"Ilistprac.Location, Ilistprac.Location, Ilistprac.Location"

: 내가 어떤 이유로, 체크 표시 내 출력, 출력이 닫기 전에, 작동합니다.

답변

4

01 필요 없음모든 그래서 당신이 전화를 응답, 나는이 오류 메시지가 얻을

string.Join(",", test); 
+0

아아 네, 4.0 버전을 사용하고 있습니다. – nhat

+0

나는이 유익에 응답하기 바로 전에. 내 출력은 다음과 같이 출력됩니다. "Ilistprac.Location, Ilistprac.Location, Ilistprac.Location"및 어떤 이유로 목록의 목록 항목이 아닙니다. – nhat

+0

'Location' 객체에'ToString()'을 오버라이드하지 않았으므로 기본 동작을 얻고 있습니다. – dlev

1

보십시오 : 당신은 괄호를 둘 필요가

string printitout = string.Join(",", test); 
+3

감사 할 수 있습니다 (당신이 닷넷 4.0을 사용하고 나타납니다 이후) : 통화가 다음과 같은 방법 또는 속성 사이의 모호를 ' 좋아, (문자열, System.Collections.Generic.IEnumerable ) '및'string.Join (string, params object []) ' – nhat

2

- ()-ToArray<Location> 후 :

string printitout = string.Join(",", test.Select(location => location.Site).ToArray()); 
+0

거의 작동하지만 오류가 발생합니다. 위의 메시지가 – nhat

+0

@nhat - 위에 수정되었습니다. – DaveShaw

+0

아 람다, 나는 그다지 좋지 않다. 그러나 나는 그것을 배우려고 노력하고있다! 실제로 IList의 whats도 제대로 출력됩니다. – nhat

2

당신이 필요로하지 않습니다 IEnumerable 당신의 Locaions 유형을 구현하는 경우 ToArray :

string printiout = String.Join(",", test); 
+0

고마워요! – nhat

관련 문제