こんにちは。
Windows10でC#のアプリケーションを開発しています。
Visual Studio 2017 Proを使っています。
###前提・実現したいこと
Listを[][]に変換したいです。
###試したこと
下記コード
###発生している問題・エラーメッセージ
Listを展開できてない
###該当のソースコード
C#
1 private static object[][] Layouttable() 2 { 3 List<List<string>> lines = new List<List<string>>(); 4 lines.Add(new List<string>() { "line1cell1", "cell2" }); 5 lines.Add(new List<string>() { "line2cell1", "cell2" }); 6 lines.Add(new List<string>() { "line3cell1", "cell2" }); 7 8 object[] oline = lines.ToArray(); 9 10 return new[] 11 { 12 new object[] { "title"}, 13 new object[] { "" }, 14 oline 15 }; 16 }
入力,出力
"title"
""
"line1cell1", "cell2"
"line2cell1", "cell2"
"line3cell1", "cell2"
です。
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Pro 2017
Microsoft .NET Framework
Version 4.6.01586
です。
よろしくお願いします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/20 01:40
2017/09/20 01:45
2017/09/20 02:31 編集
2017/09/20 03:04
2017/09/20 03:19 編集
2017/09/20 04:30
回答8件
0
ベストアンサー
c#
1 2 class Program 3 { 4 static void Main(string[] args) 5 { 6 object[][] test = LayoutTable(); 7 8 foreach (var test01 in test) 9 foreach (var test02 in test01) 10 Console.WriteLine(test02); 11 12 Console.ReadLine(); 13 } 14 15 private static object[][] LayoutTable() 16 { 17 List<List<string>> lines = new List<List<string>>(); 18 lines.Add(new List<string>() { "line1cell1", "cell2" }); 19 lines.Add(new List<string>() { "line2cell1", "cell2" }); 20 lines.Add(new List<string>() { "line3cell1", "cell2" }); 21 22 var oline = lines.Select(line => line) 23 .Select(line_child => line_child.ToArray()) 24 .ToArray(); 25 return oline; 26 } 27 } 28
こちらのサイトを参考にしました。
質問項目自体に書きミスが多い。
質問するときはコードをVisualStudioなどで試してから
書いてほしいと思います。
投稿2017/09/20 02:42
総合スコア138
0
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4 5class Program 6{ 7 class Test 8 { 9 public readonly string[] Title, Data; 10 public readonly string[][] Line; 11 12 public Test(string[] title, string[] data, string[][] line) 13 { 14 this.Title = title; 15 this.Data = data; 16 this.Line = line; 17 } 18 } 19 20 private static Test Layoutable() 21 { 22 var lines = new List<List<string>>() 23 { 24 new List<string>() { "line1cell1", "cell1" }, 25 new List<string>() { "line1cell2", "cell2" }, 26 new List<string>() { "line1cell3", "cell3" } 27 }; 28 29 var oline = lines.Select(e => e.ToArray()).ToArray(); 30 31 return new Test(new string[] { "title" }, new string[] { "" }, oline); 32 } 33 34 static void Main() 35 { 36 var t = Layoutable(); 37 38 Console.WriteLine("{0}, {1}", 39 string.Join(", ", t.Title), string.Join(", ", t.Data)); 40 41 foreach (var l in t.Line) 42 Console.WriteLine(string.Join(", ", l)); 43 } 44}
C#
1 return new[] 2 { 3 new object[] { "title"}, 4 new object[] { "" }, 5 oline 6 };
この個所から、もしかしたらこういうことかもと書き換え。
投稿2017/09/19 12:25
編集2017/09/19 12:32退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/20 01:48
2017/09/20 02:21
0
久しぶりに投稿します。
処理内容が何がしたいのかさっぱり分かりません。
このコードを質問者である、cancat さんが書かれたものか、
他の方が書いたものか分かりませんが。
まず
c#
1List<List<List<string>>> lines = new List<List<List<string>>>(); 2foreach (var line in lines)
この時点で意味不明です。書き間違え?linesなんてnewしたところなんだから
foreachループ内には入って処理はしませんが。
もう少し日本語での説明があったほうがいいかなあっと。
私が無知なだけかも。。。
投稿2017/09/19 09:59
総合スコア138
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/20 01:43
2017/09/20 02:16
2017/09/20 02:38
0
cs
1 private static object[][] Layouttable() { 2 List<List<string>> lines = new List<List<string>>(); 3 lines.Add(new List<string>() { "line1cell1", "cell2" }); 4 lines.Add(new List<string>() { "line2cell1", "cell2" }); 5 lines.Add(new List<string>() { "line3cell1", "cell2" }); 6 7 object[][] oline = lines.Select(l => l.Select(f => (object)f).ToArray()).ToArray(); 8 9 return new object[][] 10 { 11 new object[] { "title"}, 12 new object[] { "" }, 13 }.Concat(oline).ToArray(); 14 } 15 16 static void Main(string[] args) { 17 var table = Layouttable(); 18 19 foreach (var line in table) { 20 foreach (var field in line) { 21 Console.Write(field); 22 Console.Write(","); 23 } 24 Console.WriteLine(); 25 } 26 27 Console.ReadLine(); 28 } 29
objectである必要性を感じませんが、展開部分とreturn部分を調整してます。
投稿2017/09/20 04:00
総合スコア385
0
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4 5class Program 6{ 7 private static object[][] Layouttable() 8 { 9 List<List<string>> lines = new List<List<string>>(); 10 lines.Add(new List<string>() { "line1cell1", "cell2" }); 11 lines.Add(new List<string>() { "line2cell1", "cell2" }); 12 lines.Add(new List<string>() { "line3cell1", "cell2" }); 13 14 object[] oline = lines.ToArray(); 15 16 return new object[][] 17 { 18 new object[] { "title" }, 19 new object[] { "" }, 20 oline 21 }; 22 } 23 24 static void Main() 25 { 26 var t = Layouttable(); 27 28 Console.Write("{0}\n{1}\n", t[0][0], t[1][0]); 29 30 foreach (var l in (List<string>[])t[2]) 31 Console.WriteLine(string.Join(", ", l)); 32 } 33}
lines.ToArray()はList<string>[]になるので
List<string>[]でキャストすることにしました。
出力結果
title line1cell1, cell2 line2cell1, cell2 line3cell1, cell2
投稿2017/09/20 04:00
編集2017/09/20 04:04退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
List<List<string>>
を string[][]
にしたいのなら
C#
1var items = lines.Select(l => l.ToArray()).ToArray();
で良いのでは?
投稿2017/09/20 02:18
総合スコア797
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/20 02:23
2017/09/20 02:26
0
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4 5class Program 6{ 7 private static object Layoutable() 8 { 9 var lines = new List<List<string>>() 10 { 11 new List<string>() { "line1cell1", "cell2" }, 12 new List<string>() { "line1cell1", "cell2" }, 13 new List<string>() { "line1cell1", "cell2" } 14 }; 15 16 var oline = new string[lines.Count][]; 17 18 for (var i = 0; i < lines.Count; i++) 19 oline[i] = lines[i].ToArray(); 20 21 return oline; 22 } 23 24 static void Main() 25 { 26 foreach (var r in (string[][])Layoutable()) 27 Console.WriteLine(string.Join(", ", r)); 28 } 29}
objectをreturnするように変更。
投稿2017/09/19 11:42
退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/20 01:47
2017/09/20 02:16
0
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4 5namespace AAAA 6{ 7 class Program 8 { 9 private static string[][] Layoutable() 10 { 11 var lines = new List<List<string>>(); 12 13 lines.Add(new List<string>() { "line1cell1", "cell2" }); 14 lines.Add(new List<string>() { "line1cell1", "cell2" }); 15 lines.Add(new List<string>() { "line1cell1", "cell2" }); 16 17 var oline = new string[lines.Count][]; 18 19 for (var i = 0; i < lines.Count; i++) 20 oline[i] = lines[i].ToArray(); 21 22 return oline; 23 } 24 25 static void Main(string[] args) 26 { 27 var result = Layoutable(); 28 29 foreach (var l in result) 30 Console.WriteLine(string.Join(", ", l)); 31 } 32 } 33}
結果
line1cell1, cell2 line1cell1, cell2 line1cell1, cell2
[][]だとList<List<string>>だろうと思って
書き換えました。
投稿2017/09/19 10:14
退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/20 01:46
2017/09/20 02:16
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。