System.FormatException:'输入字符串的格式不正确。'

时间:2017-10-22 12:25:47

标签: c#

string[] rounds = cols[5].Split('|');

foreach (string round in rounds)
{
    //splitting each round on "^"
    string[] msText = round.Split('^');

    //create a new list in text file
    List<MatchupModel> ms = new List<MatchupModel>();

    foreach (string matchupModelTextId in msText)
    {
        // this is the line on which I am getting the error
        ms.Add(matchups.Where(x => x.Id == int.Parse(matchupModelTextId)).First());
    }

    tm.Rounds.Add(ms);
}

1 个答案:

答案 0 :(得分:0)

试试这个 - 看看matchupModelTextId是否可以解释为一个整数,只有它可以,让它添加匹配;

foreach (string matchupModelTextId in msText)
{
    int matchupId;
    if (int.tryParse(matchupModelTextId, out matchupId)) {
        ms.Add(matchups.Where(x => x.Id == matchupId).First());
    }
}