次を介して同じ基本操作を実行できます。
Tuple<double[][], string[]> GetElections()
{
var file = @"C:\Users\Deines\Documents\Election2008.txt";
var fileAsLines = File.ReadLines(file).Select(line => line.Split(','));
var dataset = fileAsLines.Select(line => new[]
{
Convert.ToDouble(line[1]),
Convert.ToDouble(line[2]),
Convert.ToDouble(line[3])
}).ToArray();
var labels = fileAsLines.Select(line => line[4]).ToArray();
return Tuple.Create(dataset, labels);
}
そうは言っても、C#開発者はこれをこのように書くことはめったにありません。結果を保持するためにカスタムタイプを作成し(名前 +値で)、そのように読む可能性が高くなります。
class ElectionResult
{
public ElecationResult(string label, double x, double y, int amount)
{
this.Label = label;
this.Point = new Point(x,y);
this.Amount = amount;
}
string Label { get; private set; }
Point Location { get; private set; }
int Amount { get; private set; }
}
IList<ElectionResult> GetElectionResults()
{
var file = @"C:\Users\Deines\Documents\Election2008.txt";
var fileAsLines = File.ReadLines(file).Select(line => line.Split(','));
return fileAsLines.Select(line => new ElectionResult(line[4],
Convert.ToDouble(line[1]),
Convert.ToDouble(line[2]),
Convert.ToInt32(line[3]))
.ToList();
}
これにより、タプル結果から配列を抽出するパターンマッチングがないため、典型的なC#開発者にとってはるかに使用可能になります。