简体   繁体   English

转换问题

[英]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??? 如何转换不带引号的字符串变量???

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. 窥探OP可能会遇到的问题,我怀疑您有一个DateTime对象,您想在查询中使用该对象与存储为字符串的日期进行比较。 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. ToString方法正在格式化日期以仅返回年和月组成部分。 Look to the MSDN article on Custom date and Time Format Strings for more. 有关更多信息,请参见MSDN文章“ 自定义日期和时间格式字符串 ”。

To extend the example, it's currently about 3pm on Sunday, November 22nd. 进一步来说,目前是11月22日星期日下午3点左右。 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. ...我会看到2009/11年印刷的内容。

Unlike "2002/03" , 2002/03 is not a string but the integer division of 2002 by 03 (= 667 ). "2002/03"不同, 2002/03不是字符串,而是2002除以03 (= 667 )的整数。

Are you looking how to convert a DateTime to a string? 您在寻找如何将DateTime转换为字符串吗?

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

This returns "2002/03" . 这将返回"2002/03"

Your problem is 2002/03 is not what you mean. 您的问题是2002/03年不是您的意思。 What are you trying to convert here? 您要在这里转换什么? 2002/03 is two integers and a division, and it's value is 2002 / 03 = 667 . 2002/03是两个整数和一个除法,其值为2002/03 2002 / 03 = 667 If you want the string "2002/03" you need to enter that string, "2002/03" . 如果需要字符串"2002/03" ,则需要输入字符串"2002/03"

I hope this made sense :) 我希望这是有道理的:)

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. 您只需要与实际字符串进行比较,就像在第二个示例中一样。

The string "2002/03" and 2002/03 are very different things. 字符串"2002/03"2002/03是非常不同的东西。 In C# there are no such things as string literals without quotes. 在C#中,没有诸如带引号的字符串文字之类的东西。 C# is not PHP :-) C#不是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). 2002/03只是一个整数除法,即2002/3 = 667(请注意,由于这是一个整数除法,因此没有小数位)。

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). 但是请记住,将==运算符应用于objectstring操作数时,其行为有些不规律(因为在执行值或引用相等时可能并不立即显而易见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM