mam IEnumerable < podwójne > chcę przekonwertować do IEnumerable < ciąg >. Problem polega na tym, że poniższy kod zgłasza wyjątek null dla instrukcji select. Co robię źle?LINQ: Konwersja IEnumable <double> do IEnumable <string> użyciu metody pomocnika
Rzeczywisty problem pojawia się przy próbie iterację zwróconego IEnumerable < ciąg >. Otrzymuję wyjątek InvalidCastException . Widzę w debuger że struny = {System.Linq.Enumerable.WhereSelectEnumerableIterator < double, string >}
private IEnumerable<string> ConvertToString(IEnumerable<double> doubles)
{
IEnumerable<string> strings = null;
if (doubles != null)
strings = doubles.Select(d => ConvertToString(d));
return strings;
}
private string ConvertToString(double d)
{
return string.Format("{0:0.00}", d);
}
Ok, I rozwiązać mój problem. Ta opóźniona realizacja Linq sprawia, że debugowanie jest trudne. Mam problem z połączeniem w górę strumienia.
ICollection<float> floats; //pretend it holds valid data
ConvertToString(floats.Cast<double>()) //<---This is naughty
Kod jak jest nie powinny rzucać żadnych ArgumentNullException. 'd' nie może mieć wartości null, ponieważ jest to typ wartości; i 'double' nie może być wartością null, ponieważ to sprawdzasz. Czy możesz dodać krótki, ale kompletny program, który pokazuje problem? – dtb