Simon Kiely
Simon Kiely

Reputation: 6070

Converting to decimal and then to varchar in linq to sql

I used to have a stored procedure which would simply call

SELECT item = CONVERT(VARCHAR(6), CONVERT(DECIMAL(5, 2), (t.aNumber/(t.aSecondNumber+ .0000000000001)) * 100)) + ' %',
FROM table t;

to get a percentage value.

I wish to update this, and call it in my linq to sql query, however I cannot figure out how to do this in the object instantiation. So, at the moment I have :

using (context db = new context(ConnectionString))
                {
                    var versions =
                                from t1 in db.table
                                select new CustomObject
                                {
                                    Id = t1.ID,
                                    Item = (t1.aNumber/(t1.aSecondNumber+ .0000000000001)) * 100,

                                };
                 }

But this will not work the same as my query above, how can I write convert it in the same way as above using the linq to sql syntax I am using, in the object instantiation ?

Thanks very much

Upvotes: 0

Views: 1124

Answers (1)

loopedcode
loopedcode

Reputation: 4893

This might work for you:

Item = (Convert.ToDecimal((t1.aNumber/(t1.aSecondNumber+ .0000000000001))) * 100m).ToString(),

Upvotes: 1

Related Questions