我正在尝试使用LinqToQuerystring构建一个Nancy OData支持应用程序。我得到了以下示例代码。它适用于任何查询网址:
获取[“/ test”] = _ =>
{ var dict =(IDictionary)Request.Query.ToDictionary();
new List<Movie>
{
new Movie
{
Title = "Matrix (The)",
ReleaseDate = new DateTime(1999, 3, 31),
DurationInMinutes = 136,
MetaScore = 73,
Director = "Wachowski Brothers",
Recommended = true
},
new Movie
{
Title = "There and Back Again, An Unexpected Journey",
ReleaseDate = new DateTime(2012, 12, 14),
DurationInMinutes = 169,
MetaScore = 58,
Director = "Peter Jackson",
Recommended = false
}
}.AsQueryable()
.LinqToQuerystring(dict);
return dict;
}
答案 0 :(得分:1)
您可以先调用ToDictionary解决此问题。
即
var dict = (IDictionary<string, object>) Request.Query.ToDictionary();
...
.LinqToQuerystring(dict);
这可能是因为LinqToQuerystring处理字典的方式,在中间窗口输出它们导致:
(IDictionary<string, object>) Request.Query
{Nancy.DynamicDictionary}
[Nancy.DynamicDictionary]: {Nancy.DynamicDictionary}
Keys: Count = 2
Values: Count = 2
(IDictionary<string, object>) Request.Query.ToDictionary()
Count = 2
[0]: {[one, one]}
[1]: {[two, 2]}
修改强>
根据您的评论,我假设您希望始终返回JSON。
如果是这样的话,你会这样做就是返回:
return Response.AsJson(dict);
这会将字典序列化为JSON。