あなたが抱えている問題の1つは、List<T>
というキーワードを付けて少し混乱させてしまった可能性があると思います。 A DataSet
は独自のものであり、このコードを読みにくくします。まず、のはそれをクリーンアップしてみましょう:
List<string> theData = new List<string>();
string s;
while ((s = sr.ReadLine()) != null)
{
if (!String.IsNullOrEmpty(s))
{
if (s[0] == '$')
{
theData.Add(s);
if (s.Contains("GPGGA"))
{
myData.Add(new DataSet(theData)); //you're passing your source by reference.
theData.Clear();
Console.WriteLine();
}
}
}
}
あなたはあなたのデータソースの値を渡す必要がありますが、List<T>
は参照型です。
List<string> theData = new List<string>();
string s;
while ((s = sr.ReadLine()) != null)
{
if (!String.IsNullOrEmpty(s))
{
if (s[0] == '$')
{
theData.Add(s);
if (s.Contains("GPGGA"))
{
List<string> bindingdata = theData.ToList();
myData.Add(new DataSet(bindingData)); //you're passing
//your source by value, because it's using a new list.
theData.Clear(); //you can now safely do this.
Console.WriteLine();
}
}
}
}
行われているすべてのDataSet
がList<string>
を受け取るコンストラクタを持っていないので、それはまだ、動作しません。 myData.Add(new DataSet(theData))
を実際のコンストラクタに変更する必要があります。興味深いことに、DataSet
はここでやっていることにとって貧しい選択かもしれません。表示されていない複数のテーブルを使用している場合を除き、DataTable
を1つだけ使用するほうがよいです。でも、そのすべてと
List<string> theData = new List<string>();
string s;
while ((s = sr.ReadLine()) != null)
{
if (!String.IsNullOrEmpty(s))
{
if (s[0] == '$')
{
theData.Add(s);
if (s.Contains("GPGGA"))
{ //we don't need this now.
//List<string> bindingdata = theData.ToList();
//make myData a DataSet so you can use datatables
// instead of attempting IEnumerable<DataSet>
DataTable foo = new DataTable("Foo");
foreach(string s in theData)
{
var y = foo.NewRow();
y[0] = s;
foo.Rows.Add(y);
}
myData.Add(foo);
theData.Clear(); //you can now safely do this.
Console.WriteLine();
}
}
}
}
あなたはDataTable
とStringBuilder
クラスに見れば、あなたは自分自身に多くの問題を節約することでしょう。あなたがしようとしているよりも、このすべてを行うためのより良い方法があります。
あなたの問題は何ですか? – Quantic
どのような値が上書きされ、何をしようとしているのかを詳しく教えてください。 – bc004346
"myData"という名前のリストに2番目のデータセットを追加すると、既存のすべてのデータが上書きされるため、最新のものとx個のコピーのみがリストに追加されます – Michael