私はこの問題を一週間一回解決しようと努力してきました。この時点で、C#言語をさらに深く浸透させることなく解決できるかどうかは疑問です。 CSVファイルを使って作業し、並べ替えて整理するだけでなく、C#の初心者でもかなり新しくなっています。CSVファイルを使って作業する
私はCSVファイルをアルファベット順にソートしようとしています。非表示にする必要がある項目を隠し、親、子、孫の要素に基づいて深度レベルを持っています。
私はそれらのいくつかでうまくいっていて、多少の作業コードを書いていますが、それらをアルファベット順に並べ替える方法と、それらが属している親と子に基づいて適切な深度レイヤを与える方法はわかりません。ここで
は私が整理しようとしてきたモックアップのCSVです:私はセミコロンで項目を区切られてきた
ID;MenuName;ParentID;isHidden;LinkURL
1;Company;NULL;False;/company
2;About Us;1;False;/company/aboutus
3;Mission;1;False;/company/mission
4;Team;2;False;/company/aboutus/team
5;Client 2;10;False;/references/client2
6;Client 1;10;False;/references/client1
7;Client 4;10;True;/references/client4
8;Client 5;10;True;/references/client5
10;References;NULL;False;/references
、私は表示する必要がある項目を表示しましたが、私はに失敗します私のようにそれらを並べ替える。
ソートは次のようになります。私はそれらを並べ替えるか、スラッシュが、どのようなコードが再現するのインデックスを取得することにより、その順序でそれらを表示しようとした
Company
About Us
Team
Mission
References
Client 1
Client 2
はどのようにすべきではありません表示され、そして、それは次のようになります。
Company
About Us
Mission
Team
Client 2
Client 1
References
を私は再帰的にidを持つ親IDと一致し、他の試みでは、コンソールの表示は次のようになります。
Company
About Us
Mission
Team
Client 2
Client 1
References
私は友人とこれを解決しようとしましたが、このコードは別の親IDを使用する別のファイルでも機能するので、彼はこの問題にどのようにアプローチするのかわかりません。このすべての上に
のみ0のインデックスやインデックスがありますので、私は、配列のインデックス彼らすることができません彼らの手紙に基づくか、私は1
のインデックス位置を入力すると、コンソールがクラッシュしていますここでが、私はそれらを並べ替えることができない最初の部分のコードです:
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(@"Navigation.csv");
string data = sr.ReadLine();
while (data != null)
{
string[] rows = data.Split(';');
int id;
int parentId;
bool ids = Int32.TryParse(rows[0], out id);
string name = rows[1];
bool pIds = Int32.TryParse(rows[2], out parentId);
string isHidden = rows[3];
string linkUrl = rows[4];
string[] splitted = linkUrl.Split('/');
if (isHidden == "False")
{
List<CsvParentChild> pIdCid = new List<CsvParentChild>()
{
new CsvParentChild(id, parentId, name, linkUrl)
};
}
data = sr.ReadLine();
}
}
}
class CsvParentChild
{
public int Id;
public int ParentId;
public string Name;
public string LinkUrl;
public List<CsvParentChild> Children = new List<CsvParentChild>();
public CsvParentChild(int id, int parentId, string name, string linkUrl)
{
Id = id;
ParentId = parentId;
Name = name;
LinkUrl = linkUrl;
string[] splitted = linkUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (splitted.Length == 1)
{
Console.WriteLine($". { name }");
}
else if (splitted.Length == 2)
{
Console.WriteLine($".... { name }");
}
else if (splitted.Length == 3)
{
Console.WriteLine($"....... { name }");
}
}
}
そしてここでは、第二部のためです:
class Program
{
static void Main(string[] args)
{
// Get the path for the file
const string filePath = @"../../Navigation.csv";
// Read the file
StreamReader sr = new StreamReader(File.OpenRead(filePath));
string data = sr.ReadLine();
while (data != null)
{
string[] rows = data.Split(';');
ListItems lis = new ListItems();
int id;
int parentId;
// Get the rows/columns from the Csv file
bool ids = Int32.TryParse(rows[0], out id);
string name = rows[1];
bool parentIds = Int32.TryParse(rows[2], out parentId);
string isHidden = rows[3];
string linkUrl = rows[4];
// Split the linkUrl so that we get the position of the
// elements based on their slash
string [] splitted = linkUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
// If item.isHidden == "False"
// then display the all items whose state is set to false.
// If the item.isHidden == "True", then display the item
// whose state is set to true.
if (isHidden == "False")
{
// Set the items
ListItems.data = new List<ListItems>()
{
new ListItems() { Id = id, Name = name, ParentId = parentId },
};
// Make a new instance of ListItems()
ListItems listItems = new ListItems();
// Loop through the CSV data
for (var i = 0; i < data.Count(); i++)
{
if (splitted.Length == 1)
{
listItems.ListThroughItems(i, i);
}
else if (splitted.Length == 2)
{
listItems.ListThroughItems(i, i);
}
else
{
listItems.ListThroughItems(i, i);
}
}
}
// Break out of infinite loop
data = sr.ReadLine();
}
}
public class ListItems
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public static List<ListItems> data = null;
public List<ListItems> Children = new List<ListItems>();
// http://stackoverflow.com/a/36250045/7826856
public void ListThroughItems(int id, int level)
{
Id = id;
// Match the parent id with the id
List<ListItems> children = data
.Where(p => p.ParentId == id)
.ToList();
foreach (ListItems child in children)
{
string depth = new string('.', level * 4);
Console.WriteLine($".{ depth } { child.Name }");
ListThroughItems(child.Id, level + 1);
}
}
}
}
本当にCSVを使用する必要がありますか? CSVは、運動の場合を除いて、これのために非常に貧しい選択と思われます。 XML、JSON、またはYAMLなどのよくサポートされたライブラリを持つ階層的なマークアップ言語は、はるかに優れた選択肢のように聞こえる。もう1つの選択肢は、ある種のSQLデータベースに入れて、SQLを使ってソートし、それを既にソートしてエクスポートすることです。あなたはSQLiteを使うことができます。 –
私は問題を解決しようとするインターンシップのインタビューのため、私は主にフロントエンドのポジションに集中しているので、ファイルをソートするように求められます(そして、CSVを使用してソリューションをテストしています)。数日前、PluralsightからC#を学び始めたので、私はそれをソートするのに失敗しました。私はまだ中間コースに到達していません。そして、これは私に可能な解決策を試してみる時間です。 そして、C#で解決するように求められています。 – Timathy
ああ。私は完全に理解する。 –