c# - Linq: Sum() non-integer values -
This is a continuation of my preਿਵise question:
I have such a question: It works fine, but I am thinking how can I handle the case where the columnatum is empty? For example, it gives me an error "input string was not in a correct format" such as int.Parse fails How can I handle this scenario satisfactorily? Use an option, var content = new list & lt; List & lt; String & gt; & Gt; {"ABC", "1", "10"}, new list & lt; String & gt; {"Abc", "new list", "list", "1", "5"}, new list & lt; String & gt; {"Cde", "1", "6"},}; Var header = content.First (); Var result = content.Skip (1) .GroupBy (s => new {code = s [headers.InxexOf ("code")], book = s [headers.InxexOf ("book"))}. Select (g = & gt; new {book = gkbook, code = g.ccode, total = g. select (s => gt; purse (s [header index ("columnitosom" )))). Yoga ()});
string. All (four .IsDigit) as pre-check:
total = live Select (S => String.IsNullOrEmpty (O [header]. IndexAff ("ColMitosum") & amp; amp; amp; amp; headers.InxexOf ("columnToSum")] All (four. IsDigit)? Int.Parse (S [headers.InexexOf ("columnToSum")): 0). SIM () will use the second
int.TryParse :
int val = 0; // ... total = g.Select (s => int.TryParse (s [headers.IndexOf ("columnToSum"), outside)? Int.Parse (s [headers.InexexOf ("columnToSum") ]): 0) .Sum ())
Comments
Post a Comment