Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I was hoping someone might help me understand why Convert.ToDecimal when used within linq is rounding a decimal and when used outside, it does not

Given the following DB:

CREATE TABLE [dbo].[Widgets](
    [ID] [int] NOT NULL,
    [WidgetName] [varchar](50) NOT NULL,
    [UnitsAvailable] [int] NOT NULL,
    [WeightInGrams] [decimal](10, 6) NULL
) ON [PRIMARY]
GO

INSERT [dbo].[Widgets] VALUES (1, N'Best thing ever', 100, CAST(10.000210 AS Decimal(10, 6)))
INSERT [dbo].[Widgets] VALUES (2, N'Next Best thing', 50, CAST(100.000151 AS Decimal(10, 6)))
INSERT [dbo].[Widgets] VALUES (3, N'The Other Model', 25, CAST(5.231651 AS Decimal(10, 6)))

CODE:

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("------Example 1--------");

        LqToSqlDataContext _ctx = new LqToSqlDataContext();

        List<Widget> inventory = (from c in _ctx.linqWidgets
                                  select new Widget()
                                  {
                                    Id = c.ID,
                                    Name = c.WidgetName,
                                    UnitsOnHand = c.UnitsAvailable,
                                    WeightInGrams = Convert.ToDecimal(c.WeightInGrams)
                                  }).ToList();

        foreach(Widget w in inventory)
        {
            Console.WriteLine(w.ToString());
        }


        Console.WriteLine("------Example 2--------");

        var _linqInventory = _ctx.linqWidgets;
        Widget temp = null;

        foreach(linqWidget lw in _linqInventory)
        {
            temp = new Widget();
            temp.Id = lw.ID;
            temp.Name = lw.WidgetName;
            temp.UnitsOnHand = lw.UnitsAvailable;
            temp.WeightInGrams = Convert.ToDecimal(lw.WeightInGrams);

            Console.WriteLine(temp.ToString());
        }



        Console.ReadLine();
    }
}

class Widget
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int UnitsOnHand { get; set; }
    public decimal WeightInGrams { get; set; }

    public override string ToString()
    {
        return this.Id + "\t" + this.Name + "\t" + this.UnitsOnHand + "\t" + this.WeightInGrams;
    }
}

Output

------Example 1--------
1       Best thing ever 100     10.0002
2       Next Best thing 50      100.0002
3       The Other Model 25      5.2317
------Example 2--------
1       Best thing ever 100     10.000210
2       Next Best thing 50      100.000151
3       The Other Model 25      5.231651
share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

up vote 3 down vote accepted

Because LinqtoSql translates Convert.ToDecimal to a sql statement like CONVERT(DECIMAL({someNumberLinqComesupWith}, 4)

share|improve this answer
add comment (requires an account with 50 reputation)

Because the C# decimal type is not the same as the decimal type in the Database.

As you can see, C# decimal is more accurate.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.