0

Is possible to write a query like below using LINQ? Is giving me the error

"cannot be applied to operands of type double and string"

key = Request.QueryString["key"];

var query = from p in db.VERSIONs where p.vr_key == key select p;
4
  • You should give us more code, but I guess that p.vr_key is of type double like your message says. Commented Mar 16, 2013 at 4:13
  • You need to parse to string the verkey Commented Mar 16, 2013 at 4:14
  • @LinusCalwell, how can I do that? Commented Mar 16, 2013 at 4:18
  • @Ravi, How can I parse to string the vr_key? Commented Mar 16, 2013 at 4:19

3 Answers 3

3

try this:

Double key = Convert.ToDouble(Request.QueryString["key"]);

var query = from p in db.VERSIONs where p.vr_key == key select p;
Sign up to request clarification or add additional context in comments.

1 Comment

Dang. Every time I bust out VS2010 to test a workable solution, someone else beats me to it.
3

Use TryParse method to make sure your string is compatible/ convertible because query string can be anything. Otherwise a user can easily break your code simply typing something for key.

double key;
if(Double.TryParse(Request.QueryString["key"], out key))
{
    var query = from p in db.VERSIONs 
                        where p.vr_key == key 
                        select p;
}

3 Comments

+1 for the even better solution. Parsing a QueryString without checking is risky.
It's only risky if you don't know whether or not the string you're parsing is the type you're looking for.
@oscilatingcretin: user can type anything on address bar and enter. ex : www.yoursite.com?key=hello
-1

Use SqlFunctions.StringConvert to convert p.vr_key.

Try this:

key = Request.QueryString["key"];

var query = from p in db.VERSIONs where SqlFunctions.StringConvert(p.vr_key) == key select p;

1 Comment

I did not give you the downvote but I'm assuming it's because you converted the database value to a string rather than parsing the key value to a double to match the database type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.