Can I add the numbers I get in a string format using the below code:
Size = a.Type == "Machines" ?string.Join(",", a.Info.DiskInfo.Select(b => b.Size)) : null,
The output of this code is: "50.00 GiB, 16.00 GiB", for example.
I decided to first remove the units from it so that I can add the numbers straight away. Therefore, I changed the code to:
Size = a.Type == "Machines" ? string.Join(",", a.Info.DiskInfo.Select(b => b.Size).Select(c => Regex.Replace(c, "[^0-9.]", ""))) : null,
Now, the output is: "50.00, 16.00"
How do I add these numbers which are in a string using LINQ, to get "66.00" as the output?
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => Convert.ToSingle(n.Trim())).Sum();You could probably re-work that to avoid thestring.Joinat the beginning.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(n => Convert.ToSingle(n.Trim())).Sum().First()at the beginning of that, ie:.First().Split(...)...Don't use.ToStringbefore it.