I will print the average price of the products in each city. My code is like this but I get “Conversion from string “” to type ‘Integer’ is not valid” error.
This is my code:
( From row In dtRead
Let xCityName = row(“City”).ToString.Trim
Group row By a = xCityName Into grp = Group
Let xCount = CStr(grp.Count)
Let xSum = grp.Sum(Function (s) CInt( s(“Price”).ToString.Trim))
Let xAverage = grp.Average(Function (av) CInt(av(“Price”).toString.Trim))
Select dtFinal.Rows.Add({a,xCount,xAverage})).CopyToDataTable
From row In dtRead
Let xCityName = row(“City”).ToString.Trim
Group row By a = xCityName Into grp = Group
Let xCount = CStr(grp.Count)
Let xSum = grp.Sum(Function (s) Convert.Toint32( s(“Price”).ToString.Trim))
Let xAverage = grp.Average(Function (av) Convert.Toint32(av(“Price”).toString.Trim))
Select dtFinal.Rows.Add({a,xCount,xAverage})).CopyToDataTable
An empty string cannot be converted into an integer. The reason of this issue could be that a row without value is processed. In excel this could be e.g. a row on end, in which another column with a space is present.
Check for this the datatable content and maybe do a cleansing filtering before processing with the LINQ