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. 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.
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".
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 :)
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. 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).
Related
I came from me last post
c# change date type format to ddmmyyyy
I have to work in a VB.Net WinForm Application I have a problem with Date or DateTime type.
I usually work with C#, but now I have this problem, I am sure is a common problem that others programmers came acrross.
I have defined in control panel my datetime format like "dd/MM/yyyy". Also y check in Regedit as it says in here
If, In my WinForm, I define a variable like this i C#
DateTime aa = DateTime.Now
DateTime.Now retrieves current date in format "dd/MM/YYYY" as spected.
But if I ask the same to VB.Net like this
Dim val As New Date
val = Date.Now
val has the current date in format "MM/dd/yyyy"
Even if i set
val = Date.Now.ToString("dd/MM/yyyy")
val keeps format as "MM/dd/yyyy"
Every date I assign to a variable Datetime, it automatically convert format "dd/MM/yyyy" to "MM/dd/yyyy".
How can i configurate VB.Net to accept Datetime type as C# Does? I need it to take it from Control Panel.
There is no actual issue here but the perceived difference is due to the fact that VB supports Date literals where C# does not. Both C# and VB support literals for numeric types, strings, Booleans, etc.
var str = "Hello World";
var num = 100;
var flag = true;
Dim str = "Hello World"
Dim num = 100
Dim flag = true
Only VB supports literals for dates though. For instance, in C#, you would have to do something like this for a hard-coded date:
var dt = new DateTime(1969, 6, 19);
You can do basically the same thing in VB:
Dim dt As New DateTime(1969, 6, 19)
but you can also do this:
Dim dt = #6/19/1969#
When you use a date literal in VB, you ALWAYS use M/dd/yyyy format. It simply couldn't make use of system settings because that would mean that the same code would behave differently on different machines.
Now, when you run a project in the debugger and use the Watch window or the like to view the value of a variable or other expression, the debugger will display a literal value if the type supports it. If the type doesn't support literals, you will see the result of calling ToString on the object. That means that the debugger will always show you something in M/dd/yyy format for a DateTime value in a VB project because it's showing you a literal Date, while what it shows you in a C# project depends on your system settings because it's the result of calling ToString on a DateTime.
This is not an issue at all though. The actual value of the DateTime variables doesn't change. They don't actually contain a format at all. They're just numbers in the system. Format is only an issue when you want to represent the value as text. When you need to do that, you'll call ToString on your value and that will honour the system settings, whether in VB or C#.
Note that I have used the terms Date and DateTime in this answer. DateTime is a .NET type while Date is an intrinsic VB data type. Both languages support literals for their intrinsic data types but only VB has an intrinsic data type for dates, which is why only VB supports literal dates. Just as int in C# and Integer in VB are aliases for the Int32 type, so Date in VB is an alias for the DateTime type, but there is no equivalent in C#.
I need to pass a string parameter to a stored procedure that represents a date but is only 10 characters long. I cannot alter the stored procedure to change the variable type or length. My only option is to use C# to ensure that the parameter is suitable.
To do this I want to convert a DateTime object to a string and remove the time. I tried using the .Date method on the DateTime object but this merely converted the time to midnight. This was ok on my local machine which represented midnight by a series of zeros but on the machine I was deploying to my code midnight was represented by 12am. This causes the stored procedure to throw an exception.
I also tried forming a substring by taking the first 10 characters after converting the DateTime object to a string. However, this gave inconsistent results due to the fact that some days and months are single digits whereas others are double digit.
For example:
12/12/2010 gets converted to "12/12/2010"
but
01/01/1900 gets converted to "1/1/1900 1" (the '1' coming from the beginning of 12:00:000AM)
You can use
string date = dt.ToShortDateString();
or (synonymous)
string date = dt.ToString("d");
or
string date = dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
The last way ensures the format even if your current culture's date-format is different.
There are many ways to do this one to try is:
var myDateTime = DateTime.Now;
var parameter = myDateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
in my website i need to read data from a XML, and some of these datas are decimal values.
Sometimes the value comes correct: 1 or 72,59and sometimes the value comes like 1.0000 or 72,590000, how is the best way to convert it to the right format:
ps: I need a string value.
Thanks!
What format are you wanting them to go to, specifically? How many decimals, etc?
If you want always 2 decimals, try a standard numeric formatting such as:
decimal value = 123.456;
Console.WriteLine("Your account balance is {0:F}.", value);
See this MSDN example for other common numeric formatting techniques.
You write that you tried
string.Format("{0:F}","1.00000");
The problem with this is that you're passing a string into the function. Numeric formatting only works on numeric data types. Convert the value to a decimal first and then format it.
Try this
public string ReformatDecimalString(string input)
{
const string formatString = //something
var decimalValue = decimal.Parse(input);
return decimalValue.ToString(formatString);
}
When you are formatting a single numeric value, it's slightly more efficient to use x.ToString(formatString) than string.Format(formatString, x). But note that the specific format string will be different in the two cases.
If your input data has decimal points (not commas) and your computer's culture uses decimal commas, you ought to be able to parse the value correctly by using CultureInfo.InvariantCulture:
var decimalValue = decimal.Parse(input, CultureInfo.InvariantCulture);
If I'm reading your answer correctly, you're trying to convert Integer values you pull from an XML file into string values without trailing zeroes ("ps: I need a string value.")
this code:
decimal test = 20.000000m
test.ToString("G29");
might do what you want
I have one string such as Date which has the value of Dates. Below is a sample value:
string Date = 05;
When Date is between 01 and 09, Date value should ignore "0". For example it should be "5".
If it is "20" means then it should not ignore "0".
How to do this in C#?
Seems like you're trying to convert a string to an integer ?
Try Convert.toInt32(date); which will return an int... then calling ToString() will give you the string representation if necessary.
You can use String.Replace() or Regex.Replace(). But it's hard to understand how to solve your problem effectively without more details/code.
you know you could use an int for this...
int month = 5; //is 5 and not 05 as an example
why do you have a string?
Also, in c# Dates have a special variable, the DateTime. You can store months, days, years, hours etc. in them. This might be related to what you are looking for.
I have this code:
startWeekDate = startWeekDate == null ? DateTimeHelpers.calcMondayDate(DateTime.Now) : DateTimeHelpers.calcMondayDate(startWeekDate.Value);
DateTime endWeekDate = startWeekDate.Value.AddDays(6);
startWeekDate is a parameter that is nullable. This works good, but I want to format it with: String.Format("{d:0}", .... ) but when I slap that around it I get error.
Cannot implicitly convert type 'string' to 'System.DateTime?
How shall I fix this problem?
/M
EDIT:
I'm trying to add this to the function instead, since it should always return dateformat without clock, but I get same error there with this code:
public static DateTime calcMondayDate(DateTime input)
{
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime monday = String.Format("{d:0}", input.AddDays(delta));
return monday;
}
Cannot implicitly convert type 'string' to 'System.DateTime'
hmm, but input is DateTime, why does it complain about it being string?
You've shown the code that doesn't have a problem, but not the code that does have a problem. Please show the code which doesn't compile. It sounds like you're trying to assign a DateTime? variable a string value, e.g.
startWeekDate = string.Format(...);
That's definitely not going to work. What would you really want it to do with the formatted value once you've got it as a string? Use it where you want a string, not where you want a DateTime?.
One thing to add - your first line can be expressed more simply:
startWeekDate = DateTimeHelpers.calcMondayDate(startWeekDate ?? DateTime.Now);
EDIT: Now you've posted your code, it's clear why it's not working - as suspected, you're trying to assign a string value to a DateTime variable.
DateTime values don't have a format. They're like numbers - they have a value which isn't inherently formatted. It's like 0x10 and 16 are the same number, just written differently.
Now it sounds like you're just trying to return the date without the time - which is better done as:
return input.AddDays(delta).Date;
The Date property returns a DateTime with the same date, but midnight as the time.
On a side note, it's a shame that .NET has such a restricted set of date/time types, so that you can't really represent the idea of a time-less date. I'm trying to fix this situation, but it'll be a while before it bears fruit...
The error is "cannot convert string to DateTime".
Whch is exactly what this line is attempting to do:
DateTime monday = String.Format("{d:0}", input.AddDays(delta));
As I said in my comment above, you format at output time. Internally a datetime is just a number, it has no concept of format. You should simply return the input.AddDays(delta)
You get the compilation error because you have declared startWeekDate as a DateTime, but string.Format returns a string. One possible remedy is to change the declaration:
string endWeekDate = string.Format("{d:0}", startWeekDate.Value.AddDays(6));
However, now endWeekDate is a string, so you migh want to change the code a bit to keep it as is, and then introduce a new variable which is the string representation of that variable. Whether or not that is a good idea depends on the context.