如何将itemList组合成单行?

时间:2017-01-25 05:11:16

标签: c# list oop join

我希望我的类型连接在一起,而不是在diff行中打印。 我如何加入我的类型加入,如"动作,冒险"而是打印成两条差异线? 我的输出打印在两个不同的行。 抱歉格式和英文不好。

我的代码:

    static void initMovies(List<Movie> mList)
    {
        Movie m;


        m = new Movie("The Great Wall", 103, "NC16", new DateTime(2016,12,29),new List<string> { "Action", "Adventure" });
        mList.Add(m);


        m = new Movie("Rogue One: A Star Wars Story", 134, "PG13", new DateTime(2016,12,15),new List<string> { "Action","Adventure"});
        mList.Add(m);


        m = new Movie("Office Christmas Party", 106, "M18", new DateTime(2017,01,15),new List<string> { "Comedy" });
        mList.Add(m);

        m = new Movie("Power Rangers", 120, "G",new DateTime(2017,1,31),new List<string>{ "Fantasy","Thriller"});
        mList.Add(m);
    }



static void displayMovie(List<Movie> mList)
{ 
    Movie m;
    Console.WriteLine("{0,-10} {1,-30} {2,-10} {3,-20} {4,-20} {5,-10}","No","Title","Duration","Genre","Classification","Opening Date");
    var gList = new List<List<string>>();
    for(int i = 0; i<mList.Count;i++)
    {
        m = mList[i];
        foreach (string value in m.GetGenreList())
        {
            Console.WriteLine("{0,-10} {1,-30} {2,-10} {3,-20} {4,-20} {5,-10}", i + 1, m.Title, m.Duration, value, m.Classification, m.openingDate);
        }
    }
}

实施例: image1

我的输出: Output

2 个答案:

答案 0 :(得分:1)

我认为您应该删除内部foreach并在此处使用string.Join

以下是一个例子:

static void displayMovie(List<Movie> mList)
{ 
    Movie m;
    Console.WriteLine("{0,-10} {1,-30} {2,-10} {3,-20} {4,-20} {5,-10}","No","Title","Duration","Genre","Classification","Opening Date");
    var gList = new List<List<string>>();
    for(int i = 0; i<mList.Count;i++)
    {
        m = mList[i];
            string genres = string.Join(", ", m.GetGenreList().ToArray());
            Console.WriteLine("{0,-10} {1,-30} {2,-10} {3,-20} {4,-20} {5,-10}", i + 1, m.Title, m.Duration, genres, m.Classification, m.openingDate);
        }
    }
}

结果:

enter image description here

希望它有所帮助!

答案 1 :(得分:1)

您必须使用In [456]: f(data[2]) Out[456]: array(False, dtype=bool) In [458]: f(list(data[2])) Out[458]: array([False, True, False], dtype=bool) In [459]: f(data[2].tolist()) Out[459]: array([False, True, False], dtype=bool) 来连接从GetGenreList()方法获得的元素,并省略内部string.join循环,如下所示:

foreach