c# - Linq: Sum() non-integer values -


This is a continuation of my preਿਵise question:

I have such a question: 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 ()});

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

  var content = new list & lt; List & lt; String & gt; & Gt; {"ABC", "1", "10"}, new list & lt; String & gt; {"Abc", "new list", "list", "1", ""}, new list & lt; String & gt; {"Cde", "1", "6"},};   

How can I handle this scenario satisfactorily?

Use an option, 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

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -