Ognjen
Ognjen

Reputation: 1195

problem with conversion

This code doesn't return data from table:

var pom = from k in dataContext.student_gods
                      where k.skgod == System.Convert.ToString(2002/03)
                      select k.id_stud;

This code does return data from table:

var pom = from k in dataContext.student_gods
                      where k.skgod== "2002/03" 
                      select k;

How to convert a string variable without quotes???

Upvotes: 0

Views: 108

Answers (5)

Michael Petrotta
Michael Petrotta

Reputation: 60902

Taking a stab at what the OP might be running into, I suspect you have a DateTime object that you'd like to use in a query to compare against a date stored as a string. If that's the case, you can modify your query to look like:

DateTime t = ...
var pom = from k in dataContext.student_gods
         where k.skgod == t.ToString("yyyy/MM")
         select k;

Here, you're formatting the date to match what you're expecting to see in your database. The ToString method is formatting the date to return just the year and month components. Look to the MSDN article on Custom date and Time Format Strings for more.

To extend the example, it's currently about 3pm on Sunday, November 22nd. If I run the following code:

DateTime t = DateTime.Now();
string s = t.ToString("yyyy/MM");
Console.WriteLine(s);

...I will see 2009/11 printed.

Upvotes: 2

Joey
Joey

Reputation: 354556

The string "2002/03" and 2002/03 are very different things. In C# there are no such things as string literals without quotes. C# is not PHP :-)

2002/03 is simply an integer division, namely 2002/3 = 667 (note that there are no decimal places, since this is an integer division).

So if you want to compare something with a string, then by all means use a string and not an arbitrary calculation result. Keep in mind though, that the == operator behaves somewhat erratically when applied to operands of object and string (since it might be not immediately obvious whether you are doing value or reference equality).

Upvotes: 0

configurator
configurator

Reputation: 41630

Your problem is 2002/03 is not what you mean. What are you trying to convert here? 2002/03 is two integers and a division, and it's value is 2002 / 03 = 667. If you want the string "2002/03" you need to enter that string, "2002/03".

I hope this made sense :)

Upvotes: 1

dtb
dtb

Reputation: 217313

Unlike "2002/03", 2002/03 is not a string but the integer division of 2002 by 03 (= 667).

Are you looking how to convert a DateTime to a string?

new DateTime(2002, 3, 1).ToString("yyyy/MM", CultureInfo.InvariantCulture)

This returns "2002/03".

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124652

How to convert a string variable without quotes???

That doesn't make sense. String literals must be surrounded with quotes, that is what makes it a string. You cannot just try to convert undeclared variables into strings by their name, it doesn't work that way. You just need to compare against an actual string, like you do in your second example.

Upvotes: 0

Related Questions