2011-10-30 37 views
0

我有一个web服务,返回一个十进制值到我的json字符串,但它取而代之的数字是400.00到400,但如果它有400.50它返回400.50这就是我要我如何在格式webservice返回舍入数字而不是小数

错误

 
{"Message":"LINQ to Entities does not recognize the method \u0027System.String ToString(System.String)\u0027 method, and this method cannot be translated into a store expression."," 

代码

 
    Products = (
    from p in db.Products where p.DepartmentId == qDepartment.Id 
    join i in db.ProductImages on p.Id equals i.ProductId into products 
    from x in products.Where(y => y.Dimension == "180X180") 
    select new Product 
    { 
     Id = p.Id, 
     Title = p.Title, 
     ShortDescription = p.ShortDescription, 
     Brand = p.Brand, 
     Model = p.Model, 
     Image = x.Path, 
     FriendlyUrl = p.FriendlyUrl, 
     SellPrice = p.SellPrice.ToString("N2")/*Error Here changed type to string*/, 
     DiscountPercentage = p.DiscountPercentage, 
     Votes = p.Votes, 
     TotalRating = p.TotalRating 
    }).ToList(); 

的值是400回到400.00

回答

3

无论你返回400还是400.00对于任何数学都不重要,我假设这是关于向用户显示小数的问题。在这种情况下,您可以将string format提供给decimal的ToString()。

decimal number = 400m; 
Console.WriteLine(number.ToString("N2")); // Outputs 400.00 

这将始终返回最多两位小数。但请注意,此时它不再是数字,而是一个字符串。如果你使用Web服务的JSON的显示和数学,然后我建议返回小数,并通过javscript格式化字符串客户端通过javscript number.toFixed(2);

+0

我试过你的例子,但我得到一个Linq To Entities错误在我的代码你知道什么可以做的工作 – ONYX

+0

我也试过Convert.ToString()同样的问题 – ONYX

+0

啊,我不知道你在使用LINQ to Entities - 让我们看看我们现在可以做些什么,现在我们已经有了这一点的信息以及 –