我有一个web api项目,在我的.net应用程序中托管我的模型,当我查询它时:
static void TitleById(MoviesService.Container container, short id)
{
try
{
MoviesService.Title title = container.Title.Where(w => w.Id == id).SingleOrDefault();
if (title != null)
{
DisplayTitle(title);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
问题不在于查询,而是来自id变量。 id变量是short类型而不是int。这是异常消息:
<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code />
<m:message xml:lang="en-US">An error has occurred.</m:message>
<m:innererror>
<m:message>Unknown function 'cast'.</m:message>
<m:type>System.NotImplementedException</m:type>
<m:stacktrace> at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()</m:stacktrace>
</m:innererror>
</m:error>
这是odata网址:
"GET http://localhost:21401/odata/Title()?$filter=cast(Id,'Edm.Int32') eq 3&$top=2"
嗯,我的entites中Id属性的类型很短。如何将其传递给查询?
谢谢,如果我需要提供更多信息,请告诉我。
答案 0 :(得分:2)
我发现在源代码中的FilterBinder.BindSingleValueFunctionCallNode中没有实现强制转换: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http.OData/OData/Query/Expressions/FilterBinder.cs
解决问题的方法之一是下载代码并自行实现。