无法从“字符串”转换为“字符”

时间:2018-12-10 21:51:32

标签: c#

我看过类似的帖子,但找不到任何能为我的案子提供答案的东西

public string DataPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "UserData.xml");

public CellModel Current
{
    get => (CellModel)cellModelBindingSource.Current;
}
protected override void OnLoad(EventArgs e)
{
    if(File.Exists(DataPath))
    {
        var xml = File.ReadAllText(DataPath);
        var sample = SampleData.FromXml(xml);
        this.cellModelBindingSource.DataSource = sample.Cells;
        MessageBox.Show($"Data read from {DataPath}");
    }            
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    var data = this.cellModelBindingSource.DataSource as DataModel[]
    var sample = new SampleData(data);
    File.WriteAllText(this.DataPath, sample.ToXml());
    MessageBox.Show($"Data written to {DataPath}");
}

错误似乎与以下行有关:

string[] entries = File.ReadAllLines();
var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(" ")[1]));
foreach (var entry in orderedEntries.Take(5))
{
    Console.WriteLine(entry);
}

它说它不能从“ string”转换为“ char”,我认为这只能按字符分割,有没有办法我可以更改它以保留我想要的空间使其具有相同的功能。

*编辑,我并不是说要把它作为副本,也不知道“定界符”就更不用说这是我的问题了。抱歉浪费您的时间。

1 个答案:

答案 0 :(得分:1)

更改此

var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(" ")[1]));

对此

var orderedEntries = entries.OrderByDescending(x => int.Parse(x.Split(' ')[1]));