Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq to SQL中的常量表达式未正确编译_Sql_Linq_Expression_Constant Expression - Fatal编程技术网

Linq to SQL中的常量表达式未正确编译

Linq to SQL中的常量表达式未正确编译,sql,linq,expression,constant-expression,Sql,Linq,Expression,Constant Expression,我有两个表(mysql),具有主-细节关系,我想在Linq中查询(然后在LinqPad中进行实验)。问题是LINQtoSQL无法为以下查询生成正确的结果或SQL语句 from m in masters select new { m.Code, m.Total, Value = m.details.Sum(d => d.Qty * d.Price * (1 - 6/100)) } 还有一个 from m in masters select new { m.Code,

我有两个表(mysql),具有主-细节关系,我想在Linq中查询(然后在LinqPad中进行实验)。问题是LINQtoSQL无法为以下查询生成正确的结果或SQL语句

from m in masters
select new {
   m.Code,
   m.Total,
   Value = m.details.Sum(d => d.Qty * d.Price * (1 - 6/100))
}
还有一个

from m in masters
select new {
   m.Code,
   m.Total,
   Value = m.details.Sum(d => d.Qty * d.Price * 0.94)
}

第一个查询不会产生正确的结果,因为后者是正确的,在我签入LinqPad后的问题存在于编译成
1.0
(1-6/100)
中。有人能解释为什么吗?

第一个表达式使用整数数学,因此是正确的,只是不是您所期望的。您假设C#将从整数文本推断浮点语义。第二个表达式是一个简单的双文本

在整数数学中,
6/100
是0,因此
(1-6/100)
是1。您需要通过将分子或分母设置为双文本,强制除法表达式使用双值

相反,请尝试:

Value = m.details.Sum(d => d.Qty * d.Price * (1 - 6/100.0))

注意
.0
使其成为一个双重表达式。

哦,没错,我完全忘记了这一点。谢谢。