LINQ to Entities无法识别方法'Double ToDouble(System.String)'方法,并且此方法无法转换为存储表达式

时间:2018-04-18 02:36:00

标签: c# linq

我目前正在将一个旧程序员的旧系统存储过程转换为linq,但我无法弄清楚导致此错误的原因。代码如下:

控制器:

$msg = "Hello {::first_name::} {::last_name::},
 How are you? 
 Your organisation is {::organisation::}";

$msg_array = explode(" ", $msg);
foreach ($msg_array as $str) {
    if (strpos($str, "{::") !== false) {
        $field_str = get_string_between($str, "{::", "::}");
        $field_value = $bean->$field_str; //Logic that gets the value of the field
        $msgStr .= $field_value . " ";
    } else {
        $msgStr .= $str . " ";
    }

}

function get_string_between($string, $start, $end)
{
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

型号:

var ObligationRequestDetailsTotalCostByOoe = (from p in db.iBudget_ObligationRequestDetails
where p.ooe_general_id != null && p.is_approved == 1
group p by p.ooe_general_id into g
select new
{
id = g.Key,
amount = g.Sum(p => Convert.ToDouble(p.amount))
}).ToList();

我尝试将 Convert.ToDouble()更改为 Double.Parse(),但此错误'Single Parse(System.String)'显示。 我尝试在 p.amount 之后添加'System.Globalization.CultureInfo.InvariantCulture'但仍然存在错误。

我在这里做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要使用AsEnumerable以便在从数据库返回后使用内存中的记录。然后,您就可以使用Convert.ToDouble

var ObligationRequestDetailsTotalCostByOoe = (from p in db.iBudget_ObligationRequestDetails
where p.ooe_general_id != null && p.is_approved == 1
group p by p.ooe_general_id into g
select g) // <-- db call
.AsEnumerable() //<-- memory call
.Select(g => new {
    id = g.Key,
    amount = g.Sum(p => Convert.ToDouble(p.amount))
})
.ToList();