0

在下面的代码中,评价生成错误

string[] allLines = File.ReadAllLines(@"Ratings.csv");

var parsed = from line in allLines
            let row = line.Split(';')
             select new
             {
                 UserId = row[0],
                 ItemId = row[1],
                 rating = row[3]
            };
var Rating = parsed.Select(x => new AddRating (x.UserId, x.ItemId,x.rating));

client.Send(new Batch(Rating));


var detailViews = parsed.Select(x => new AddDetailView(x.UserId, x.ItemId,x.rating ));
4

2 回答 2

0
String st = "85.78";
Double db = Convert.ToDouble(st);

//Or With Error Hndler

try
        {
            string st = "85.78";
            Double db = Convert.ToDouble(st);
        }
catch (FormatException)
        {

            // Your error handler 

        }
于 2018-10-25T15:58:37.397 回答
0

例外是告诉您问题所在。您的构造函数期待双打,并且您将其传递给字符串。为了修复它,您必须将字符串输入解析为双精度。

编写代码的方式,您必须更改使用.Select语句的方式,以便以适当的错误处理方式对其进行解析。

我建议交换.Selecta foreach,然后解析每个属性,然后实例化你的类。

foreach (var item in parsed)
{
    double userId = 0;
    double itemId = 0;
    double rating = 0;
    double.TryParse(item.UserId, out userId);
    double.TryParse(item.ItemId, out itemId);
    double.TryParse(item.rating, out rating);

    var rating = new AddRating(userId, itemId, rating);
    //**** do whatever you want with the new object
}
于 2018-10-25T16:01:04.983 回答