linq - C# string to number, TryParse returns true when Parse fails -
i have column in database (sql server) storing averages varchar.
values not valid numbers, can : 0
, 12
, 5.5
, .4
, 4.0
, 6.
, 5.21428571428571428571428571428571428571
, x
, fail
, or empty text.
when assigning variable linq, want display :
success
if average valid number , >= 4failed
if average valid number , < 4- the raw text if average not valid number
the code :
myresults = mydatacontext.mydbfunction() double note; in myresults select new mainreportmodel() { id = a.id, lastname = a.nom, firstname = a.prenom, average = a.moyenne, result = double.tryparse(a.moyenne.trim(), numberstyles.number, cultureinfo.invariantculture, out note) ? (double.parse(a.moyenne.trim(), numberstyles.number, cultureinfo.invariantculture) < 4 ? "failed" : "success") : a.moyenne }
but parse fails.
when replacing double.parse(a.moyenne.trim()
hardcoded value (for example double.parse("3.3"
) works fine.
so seems 1 of values of a.moyenne.trim()
causing parse fail.
how can fail if tryparse returned true?
edit : here error message :
could not translate expression 'value(myprojectweb.models.myprojectdatacontext).mydbfunction().select(a => new mainreportmodel() {id = a.id, lastname = a.nom, firstname = a.prenom, average = a.moyenne, result = iif(tryparse(a.moyenne.trim(), number, invoke(value(system.func`1[system.globalization.cultureinfo])), invoke(value(system.func`1[system.double]))), iif((parse(a.moyenne.trim(), number, invoke(value(system.func`1[system.globalization.cultureinfo]))) < 4), invoke(value(system.func`1[system.string])), "success"), ((a.moyenne) + "</span>"))})' sql , not treat local expression.
you can't perform tryparse on sql server, if not doing further selection criteria, can try pulling data sql , parsing on client.
eg
myresults = mydatacontext.mydbfunction() double note; var list =(from in myresults select new { a.id, a.nom, a.prenom, a.moyenne, }).tolist(); var finalresults = (from in list select new mainreportmodel() { id = a.id, lastname = a.nom, firstname = a.prenom, average = a.moyenne, result = double.tryparse(a.moyenne.trim(), numberstyles.number, cultureinfo.invariantculture, out note) ? (double.parse(a.moyenne.trim(), numberstyles.number, cultureinfo.invariantculture) < 4 ? "failed" : "success") : a.moyenne }
Comments
Post a Comment