试着串起来。加入一个IList

时间:2012-04-11 19:12:53

标签: linq c#-4.0 interface

我正在尝试将第一个示例http://www.dotnetperls.com/convert-list-string实现到我的方法中,但是我很难匹配该方法的第二个参数:

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(”,“,test);”工作之前,我用一个复选标记关闭它,由于某种原因我的输出,输出:

“Ilistprac.Location,Ilistprac.Location,Ilistprac.Location”

由于某种原因而不是列表中的内容。

4 个答案:

答案 0 :(得分:4)

您根本不需要ToArray()(因为看起来您正在使用.Net 4.0),因此您可以拨打电话

string.Join(",", test);

答案 1 :(得分:2)

您需要在()之后加上括号 - ToArray<Location>

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

答案 2 :(得分:2)

如果您的Locaions类型实施IEnumerable,则不需要ToArray

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

答案 3 :(得分:1)

尝试:

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