如何使用linq to entities获取类型为float的列
How to get a column which type is float using linq to entities
我是实体的 EF 和 Linq 新手。我想获得一个名称为 "Value" 的列,该列的类型是浮点数。我的代码如下所示。因此,当我调用下面的方法时,我在 double price 行出现错误......输入字符串的格式不正确。有没有办法将 return 列 "Value" 作为双列?或者如何将其转换为 Double。提前致谢。
public double calculate_price(int code, int quatity)
{
using( var context = new DryTypeEntities())
{
var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value) ;
string something = result.ToString();
double price = Convert.ToDouble(something);
double quant = Convert.ToDouble(quatity);
double total = price * quant;
return total ;
}
}
select 不会 return 您期望的值。
改为。
var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value).FirstOrDefault() ;
您可能应该检查结果是否为空。
这 returns 是 Value
的集合:
var result = context.Table_Products
.Where(p => p.Code == 3)
.Select(p => p.Value);
然后,这会将您的集合的字符串表示形式存储在 something
中(不是您想要的):
string something = result.ToString();
// System.Collections.Generic.List`1[System.Int32] or something equally unhelpful
无法转换为价格,因此失败:
double price = Convert.ToDouble(something); // uh, no.
相反,一个选项是获取第一条记录:
var result = context.Table_Products
.Where(p => p.Code == 3)
.Select(p => p.Value);
.First();
或者,如果保证 "code" 3 恰好匹配:
var result = context.Table_Products
.Where(p => p.Code == 3)
.Select(p => p.Value);
.Single();
现在 result
包含一个 Value
,您可以使用该单个值。
还有其他事情需要考虑 (即,如果没有匹配的记录怎么办,如果有多个记录要迭代怎么办,等等),但这应该让你继续再次.
最后一点 - consider using decimal
for money values 而不是 double
。
此错误意味着 A) something
是字符串而不是双精度型,并且 B) something
的内容格式不正确,无法转换为双精度型。
你还没有展示什么东西 contains
但那是你应该看的东西。
所以猜测你想要什么,你可能想做这样的事情:
using( var context = new DryTypeEntities())
{
var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value) ;
foreach (var r in result)
{
double price = Convert.ToDouble(r);
// There is no quatity returned by your query
}
}
注意 1:您的查询专门选择了 Value
,因此结果仅包含值列。查询没有返回 quatity
值。
注意 2:您的查询 returns 一个集合(一组零个或多个元素),而不仅仅是一个元素。我认为这才是您真正想要的:
using( var context = new DryTypeEntities())
{
var result = context.Table_Products.FirstOrDefault(p => p.Code == 3);
return result.Value * result.quantity;
}
我是实体的 EF 和 Linq 新手。我想获得一个名称为 "Value" 的列,该列的类型是浮点数。我的代码如下所示。因此,当我调用下面的方法时,我在 double price 行出现错误......输入字符串的格式不正确。有没有办法将 return 列 "Value" 作为双列?或者如何将其转换为 Double。提前致谢。
public double calculate_price(int code, int quatity)
{
using( var context = new DryTypeEntities())
{
var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value) ;
string something = result.ToString();
double price = Convert.ToDouble(something);
double quant = Convert.ToDouble(quatity);
double total = price * quant;
return total ;
}
}
select 不会 return 您期望的值。
改为。
var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value).FirstOrDefault() ;
您可能应该检查结果是否为空。
这 returns 是 Value
的集合:
var result = context.Table_Products
.Where(p => p.Code == 3)
.Select(p => p.Value);
然后,这会将您的集合的字符串表示形式存储在 something
中(不是您想要的):
string something = result.ToString();
// System.Collections.Generic.List`1[System.Int32] or something equally unhelpful
无法转换为价格,因此失败:
double price = Convert.ToDouble(something); // uh, no.
相反,一个选项是获取第一条记录:
var result = context.Table_Products
.Where(p => p.Code == 3)
.Select(p => p.Value);
.First();
或者,如果保证 "code" 3 恰好匹配:
var result = context.Table_Products
.Where(p => p.Code == 3)
.Select(p => p.Value);
.Single();
现在 result
包含一个 Value
,您可以使用该单个值。
还有其他事情需要考虑 (即,如果没有匹配的记录怎么办,如果有多个记录要迭代怎么办,等等),但这应该让你继续再次.
最后一点 - consider using decimal
for money values 而不是 double
。
此错误意味着 A) something
是字符串而不是双精度型,并且 B) something
的内容格式不正确,无法转换为双精度型。
你还没有展示什么东西 contains
但那是你应该看的东西。
所以猜测你想要什么,你可能想做这样的事情:
using( var context = new DryTypeEntities())
{
var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value) ;
foreach (var r in result)
{
double price = Convert.ToDouble(r);
// There is no quatity returned by your query
}
}
注意 1:您的查询专门选择了 Value
,因此结果仅包含值列。查询没有返回 quatity
值。
注意 2:您的查询 returns 一个集合(一组零个或多个元素),而不仅仅是一个元素。我认为这才是您真正想要的:
using( var context = new DryTypeEntities())
{
var result = context.Table_Products.FirstOrDefault(p => p.Code == 3);
return result.Value * result.quantity;
}