三元运算符对于 linq-to-sql 查询无效吗?
我试图在 JSON 响应中显示可为空的日期时间。在我的 MVC 控制器中,我运行以下查询:
var requests =
(from r in _context.TestRequests
where r.scheduled_time == null && r.TestRequestRuns.Count > 0
select new
{
id = r.id,
name = r.name,
start = DateAndTimeDisplayString(r.TestRequestRuns.First().start_dt),
end = r.TestRequestRuns.First().end_dt.HasValue
? DateAndTimeDisplayString(r.TestRequestRuns.First().end_dt.Value)
: string.Empty
});
当我运行 requests.ToArray()
时,出现以下异常:
Could not translate expression '
Table(TestRequest)
.Where(r =>
((r.scheduled_time == null) AndAlso (r.TestRequestRuns.Count > 0)))
.Select(r => new <>f__AnonymousType18`4(id = r.id, name = r.name,
start = value(QAWebTools.Controllers.TestRequestsController).
DateAndTimeDisplayString(r.TestRequestRuns.First().start_dt),
end = IIF(r.TestRequestRuns.First().end_dt.HasValue,
value(QAWebTools.Controllers.TestRequestsController).
DateAndTimeDisplayString(r.TestRequestRuns.First().end_dt.Value),
Invoke(value(System.Func`1[System.String])))))'
into SQL and could not treat it as a local expression.
如果我注释掉 end =
行,则一切似乎正确运行,所以它似乎不是使用我本地的DateAndTimeDisplayString
方法,所以我唯一能想到的是Linq to Sql不喜欢三元运算符?我想我以前使用过三元运算符,但我不记得我是在这个代码库还是另一个代码库(使用 EF4 而不是 L2S)中这样做的。
这是真的吗,还是我错过了其他问题?
I am trying to display a nullable date time in my JSON response. In my MVC Controller I am running the following query:
var requests =
(from r in _context.TestRequests
where r.scheduled_time == null && r.TestRequestRuns.Count > 0
select new
{
id = r.id,
name = r.name,
start = DateAndTimeDisplayString(r.TestRequestRuns.First().start_dt),
end = r.TestRequestRuns.First().end_dt.HasValue
? DateAndTimeDisplayString(r.TestRequestRuns.First().end_dt.Value)
: string.Empty
});
When I run requests.ToArray()
I get the following exception:
Could not translate expression '
Table(TestRequest)
.Where(r =>
((r.scheduled_time == null) AndAlso (r.TestRequestRuns.Count > 0)))
.Select(r => new <>f__AnonymousType18`4(id = r.id, name = r.name,
start = value(QAWebTools.Controllers.TestRequestsController).
DateAndTimeDisplayString(r.TestRequestRuns.First().start_dt),
end = IIF(r.TestRequestRuns.First().end_dt.HasValue,
value(QAWebTools.Controllers.TestRequestsController).
DateAndTimeDisplayString(r.TestRequestRuns.First().end_dt.Value),
Invoke(value(System.Func`1[System.String])))))'
into SQL and could not treat it as a local expression.
If I comment out the end =
line, everything seems to run correctly, so it doesn't seem to be the use of my local DateAndTimeDisplayString
method, so the only thing I can think of is Linq to Sql doesn't like Ternary operators? I think I've used ternary operators before, but I can't remember if I did it in this code base or another code base (that uses EF4 instead of L2S).
Is this true, or am I missing some other issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修改 DateAndTimeDisplayString 以接受不同的参数类型并进行如下查询:
这样,您可以在代码中执行三元操作。
顺便说一句,实际上这部分看起来很糟糕,因为三元被转换为 IIF 并且似乎已被处理,也许尝试一个空字符串:
Modify the DateAndTimeDisplayString to accept a different argument type and have a query like this:
This way, you can do the ternary stuff in your code.
By the way, it's actually this part which looks bad, because the ternary gets translated to IIF and seems to be handled, maybe try a null string:
您是否考虑过使用空合并操作? (??)
您的代码将如下所示:
您可以修改 DateAndTimeDisplayString 方法,以在传递 null 时返回 null。
Have you considered using the Null coalescing operationg? (??)
Your code would then look like this:
You could modify your DateAndTimeDisplayString method to return null if passed null.