【发布时间】:2015-01-25 23:36:05
【问题描述】:
我正在使用 .NET4.5 和 VS2013,我有这个从 db 获取 dynamic 结果的查询。
dynamic topAgents = this._dataContext.Sql(
"select t.create_user_id as \"User\", sum(t.netamount) as \"Amount\" from transactiondetail t where t.update_date > sysdate -7 group by t.create_user_id")
.QueryMany<dynamic>();
以下语句失败并出现编译错误Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
甚至不允许我运行它
topAgents.ToList().Select(agent => new
{
User = agent.User != null ? string.Format("{0}", agent.User).Replace("CORPNTGB\\", "") : null,
Amount = agent.Amount
});
虽然这个foreach 工作得很好。
var data = new List<List<object>>();
foreach (dynamic agent in topAgents)
{
data.Add(new List<object>
{
agent.User != null ? string.Format("{0}", agent.User).Replace("CORPNTGB\\", "") : null,
agent.Amount
});
}
在我topAgents.ToList() 之后,我认为它们可以被解释为等价,是因为我明确声明var data = new List<List<object>>(); 编译器允许第二条语句吗?
为什么编译器不允许 LINQ 选择,但允许每个`?
【问题讨论】:
-
topAgents必须是dynamic吗?如果您改用var,它是否有效?