防止数字在 linq-to-sql 中转换为科学记数法
Prevent number from being converted to Scientifc Notation in linq-to-sql
我有一个从数据库中获取数据的查询。我有一个包含类型 string
的 属性 Price
的 DTO。
var query = (from Users in _db.Users
join pricing in _db.Prices
select new {
Username = Users.Name,
Price = pricing.Currency + " " + pricing.Amount
} ).AsEnumerable().Select(x => new UsersPrice
{
Username = x.Username,
Price = x.Price
});
请注意,pricing.Amount
在我的实体中是双精度类型,而在 SQL 服务器中的字段是浮点型。
数据库中的实际金额/值是 12 565 467
但在上面的查询中它返回我的是 1.25655e+007
。我怎样才能防止这种情况发生?我想要返回数据库中的实际值。请帮忙。
数据库中的实际值是一个数字。数字不包括格式 - 它们 只是数字。唯一相关的格式是当您将其转换为字符串时,即
Price = pricing.Currency + " " + pricing.Amount
所以;如果格式对您很重要,您必须 在那个时候 告诉它您想要什么格式,通常是使用 ToString()
指定特定格式和文化。
为了防止该工具尝试将其转换为 TSQL(这将不起作用),您可能希望将 "get the data" 部分与 "format the data" 部分分开,即
var query = (from Users in _db.Users
join pricing in _db.Prices
select new {
Username = Users.Name,
pricing.Currency, pricing.Amount
}).AsEnumerable().Select(x => new UsersPrice
{
Username = x.Username,
Price = x.Currency + " " + x.Amount.ToString(...) // your choices here
});
这里的相关位是在 ORM 查询中我刚刚选择了列,after AsEnumerable()
我有 格式化 它们的代码。
我有一个从数据库中获取数据的查询。我有一个包含类型 string
的 属性 Price
的 DTO。
var query = (from Users in _db.Users
join pricing in _db.Prices
select new {
Username = Users.Name,
Price = pricing.Currency + " " + pricing.Amount
} ).AsEnumerable().Select(x => new UsersPrice
{
Username = x.Username,
Price = x.Price
});
请注意,pricing.Amount
在我的实体中是双精度类型,而在 SQL 服务器中的字段是浮点型。
数据库中的实际金额/值是 12 565 467
但在上面的查询中它返回我的是 1.25655e+007
。我怎样才能防止这种情况发生?我想要返回数据库中的实际值。请帮忙。
数据库中的实际值是一个数字。数字不包括格式 - 它们 只是数字。唯一相关的格式是当您将其转换为字符串时,即
Price = pricing.Currency + " " + pricing.Amount
所以;如果格式对您很重要,您必须 在那个时候 告诉它您想要什么格式,通常是使用 ToString()
指定特定格式和文化。
为了防止该工具尝试将其转换为 TSQL(这将不起作用),您可能希望将 "get the data" 部分与 "format the data" 部分分开,即
var query = (from Users in _db.Users
join pricing in _db.Prices
select new {
Username = Users.Name,
pricing.Currency, pricing.Amount
}).AsEnumerable().Select(x => new UsersPrice
{
Username = x.Username,
Price = x.Currency + " " + x.Amount.ToString(...) // your choices here
});
这里的相关位是在 ORM 查询中我刚刚选择了列,after AsEnumerable()
我有 格式化 它们的代码。