views:

288

answers:

2

Hello,

I'm trying to convert a standard SQL query to a LINQ to SQL query.

I was wondering what the LINQ equivalent of the T-SQL function STR() is?

STR (T-SQL)

Returns character data converted from numeric data.

Syntax STR(float_expression[, length[, decimal]])

Arguments float_expression Is an expression of approximate numeric (float) data type with a decimal point. Do not use a function or subquery as the float_expression in the STR function. length Is the total length, including decimal point, sign, digits, and spaces. The default is 10. decimal Is the number of places to the right of the decimal point. Return Types char

I'm trying to explicitly set the number of decimal places for a field.

Please and thank you!

(I've tried Googling around, but Google keeps on translating str as "string" even with double quotes aroud str)

EDIT

I've tried .ToString("N2") which is exactly what I want, but LINQ isn't able to translate it.

EDIT

Clarification, I have a SQL field that is a double. I have to append/truncate to 2 decimal places. After that, I will append it with an additional string. So therefore, I believe I do need to convert it to a string. This is because I need to compare that generated string with another field that is of type varchar. (I know this is idiotic because they should be the same type, etc, but I can't change the design of the database).

A: 

The first thing that comes to mind is ToString().

See the link for some examples of how to format it to your liking, or refer to the following:


However, your statement here leads me to question whether you really want a string or not:

I'm trying to explicitly set the number of decimal places for a field.

Perhaps you could clarify what exactly you're trying to do?

lc
I've tried .ToString("N2") which is exactly what I want, but LINQ isn't able to translate it.
Huy Tran
Why don't you take the number as is from the database, then convert it to a string when you go to display it?
lc
Hmmm after thinking about it, I should be able to do that. Thanks for the help.
Huy Tran
+3  A: 

Huy,

There is a product (which I have no association with) that translates T-SQL queries into Linq queries. The name is Linqer (http://www.sqltolinq.com/). It's very inexpensive and has been a help to me in converting some of my more difficult queries.

Randy

Randy Minder
Using Linqer, I typed in the following query:Select Str(Id,2) from Master.EnterpriseLinqer created the following:from t in db.Master_Enterpriseselect new { Column1 = string.Format("{0:0}",t.ID)}
Randy Minder
string.Format works. Thank you!http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
Huy Tran