trainreq
trainreq

Reputation: 381

How to parse a string as a float in a LINQ query?

I'm writing a molar mass calculator which will display information about a chemical formula passed in by the user, such as the atomic number of the elements, their symbols, and their boiling and melting points, among other things.

The information is displayed in a DataGridView which has a DataTable as the DataSource. I've also built a class called Element which, perhaps unsurprisingly, represents an Element. It just holds a bunch of string properties to represent element information, such as atomic number, boiling point, etc.

The CSV file from which I've pulled the element information has some empty entries because not everything is known about every element. I've replaced the entries with the word "unknown," which explains why every property in the Element class is a string.

Currently, for the columns that require non-string sorting, I'm using LINQ to order the collection that builds the DataTable. For example:

        private void SortByAtomicMass()
        {
            var elementsByAtomicMass = (from el in Elements
                                        orderby float.Parse(el.Mass)
                                        select new
                                        {
                                            el.Name,
                                            el.AtomicNumber,
                                            el.Symbol,
                                            el.Mass,
                                            el.CPKHexColor,
                                            el.Electronegativity,
                                            el.State,
                                            el.BondingType,
                                            el.MeltingPoint,
                                            el.BoilingPoint,
                                            el.GroupBlock,
                                            el.YearDiscovered
                                        }).ToList<dynamic>();
            GenerateNewDataTable(elementsByAtomicMass);
        }

This works as intended - it sorts the atomic mass column as you would expect. I tried the same sort of approach for the melting point column sort:

        private void SortByMeltingPoint()
        {
            var elementsByMeltingPoint = (from el in Elements
                                          orderby float.TryParse(el.MeltingPoint, out _)
                                          select new
                                          {
                                              el.Name,
                                              el.AtomicNumber,
                                              el.Symbol,
                                              el.Mass,
                                              el.CPKHexColor,
                                              el.Electronegativity,
                                              el.State,
                                              el.BondingType,
                                              el.MeltingPoint,
                                              el.BoilingPoint,
                                              el.GroupBlock,
                                              el.YearDiscovered
                                          }).ToList<dynamic>();
            GenerateNewDataTable(elementsByMeltingPoint);
        }

This fails, and orders in a seemingly random way. How can I fix this, while staying in the realm of a LINQ approach? I'm trying to improve my skills in that area.

Upvotes: 2

Views: 516

Answers (1)

TheGeneral
TheGeneral

Reputation: 81543

You are ordering by whether it is a float, not by the value.

Maybe you want something like this:

orderby float.TryParse(el.MeltingPoint, out var value) ? value : float.MinValue 

If it's not a float, put it at the bottom.


TryParse

Converts the string representation of a number to its single-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.

Upvotes: 6

Related Questions