c# - Linq to SQL search for wildcard in double -
in linq sql trying search value of type double:
var q = in db.gettable<hkks_medlemmer>() g in db.hkks_get_grader_kun_grad(convert.toint32(a.skif)) a.aktiv == true && (a.skif.tostring().startswith(searchvalue) || sqlmethods.like(a.navn, "%" + searchvalue + "%") || sqlmethods.like(a.mellemnavn, "%" + searchvalue + "%") || sqlmethods.like(a.efternavn, "%" + searchvalue + "%") || sqlmethods.like(a.adresse, "%" + searchvalue + "%") || sqlmethods.like(a.telefon, "%" + searchvalue + "%") || sqlmethods.like(a.mobil, "%" + searchvalue + "%") || sqlmethods.like(a.postnr, "%" + searchvalue + "%") || sqlmethods.like(a.town, "%" + searchvalue + "%") || sqlmethods.like(a.mail, "%" + searchvalue + "%")) orderby a.navn select new { grad = g.grad, skif = a.skif, navn = a.navn, mellemnavn = a.mellemnavn, efternavn = a.efternavn, adresse = a.adresse, telefon = a.telefon, mobil = a.mobil, postnr = a.postnr, town = a.town, mail = a.mail, picture = "http://www.hkks.dk/" + a.picture, age = db.fn_getage((a.cpr), datetime.now), cpr = a.cpr };
i have tried this:
var u = in db.gettable<hkks_medlemmer>() g in db.hkks_get_grader_kun_grad(convert.toint32(a.skif)) .where (p => object.equals(a.skif, searchvalue)) orderby a.navn select new { grad = g.grad, skif = a.skif, navn = a.navn, mellemnavn = a.mellemnavn, efternavn = a.efternavn, adresse = a.adresse, telefon = a.telefon, mobil = a.mobil, postnr = a.postnr, town = a.town, mail = a.mail, picture = "http://www.hkks.dk/" + a.picture, age = db.fn_getage((a.cpr), datetime.now), cpr = a.cpr };
but none of them giving me correct result, if searching 994 , expecting result give me record of 99469.
any suggestions?
from comment .startswith
method generates following sql:
(convert(nvarchar(30), [t0].[skif], 2) @p11)
you can use following sql illustrate problem:
declare @val float set @val = 99469 select convert(nvarchar(30), @val, 2)
if run sql, you'll see output string not start "994":
9.946900000000000e+004
in order convert 99469 string without using e+
notation, can rewrite statement this:
convert.toint32(a.skif).tostring().startswith(searchvalue)
that generate following sql, should solve problem:
where (convert(nvarchar,convert(int,[t0].[skif]))) @p11
it nice if declare this:
a.skif.tostring("####").startswith(searchvalue)
but .tostring()
method doesn't support in linq sql (i got method 'system.string tostring(system.string)' has no supported translation sql.
error message when tried).
also, there may other workarounds. ef4 there may in sqlfunctions
allows conversion in 1 step, without converting int
(or bigint
, if necessary). really, linq sql needs generate statement 0 last parameter, instead of 2 (i.e., convert(nvarchar(30), @val, 0)
).
hope helps.
Comments
Post a Comment