我正在尝试创建一个linq查询来执行此查询。 “附加数据”列是nvarchar(20) - 因此linq将其作为字符串
读取这在SSMS中运行良好
select SUM(CAST(AdditionalData as smallmoney)) from TransTable
where ActionID = @actID and UserID = uID;
这是我在linq版本上失败的尝试(我猜猜道,Decimal.Parse()无法从linq转换为sql)
(from a in Context.TransTable
where a.ActionID == action.ActionID && a.UserID == (long)userId
select decimal.Parse(a.AdditionalData)).Sum();
答案 0 :(得分:2)
如果结果集不是太大,您可以解析客户端上的值。
decimal value;
var sum = (from a in Context.TransTable
where a.ActionID == action.ActionID && a.UserID == (long)userId
select a.AdditionalValue).ToList().
Select(x => decimal.TryParse(x, out value) ? value : 0).Sum();