Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting to Json response from List<object> #45

Closed
shivu333 opened this issue Dec 1, 2014 · 2 comments
Closed

Converting to Json response from List<object> #45

shivu333 opened this issue Dec 1, 2014 · 2 comments

Comments

@shivu333
Copy link

shivu333 commented Dec 1, 2014

Hi, I am trying to build an Nancy OData support app using LinqToQuerystring. I got below sample code. it is working for any query url like: http:/test/?$filter=Recommendede eq true. variable tests holding right query result but i am finding difficulty in converting the result(tests) into Json response.
thanks in advance.

Get["/test"] = _ =>
var tests =
new List
{
new Movie
{
Title = "Matrix (The)",
ReleaseDate = new DateTime(1999, 3, 31),
DurationInMinutes = 136,
MetaScore = 73,
Director = "Wachowski Brothers",
Recommended = true
},
new Movie
{
Title = "Avatar",
ReleaseDate = new DateTime(2009, 12, 17),
DurationInMinutes = 162,
MetaScore = 83,
Director = "James Cameron",
Recommended = false

                             },
                     }.AsQueryable()                //.LinqToQuerystring("?$orderby=Title");
                      .LinqToQuerystring((IDictionary<string, object>)this.Context.Request.Query);
@xt0rted
Copy link
Contributor

xt0rted commented Dec 2, 2014

I'm using this with Nancy backing an Angular frontend like so

public static class RepositoryExtensions
{
    public static dynamic LinqToQuerystring<T>(this IQueryable<T> query, Uri url, bool forceDynamicProperties = false, int maxPageSize = -1)
    {
        var queryString = url.Query.Replace('+', ' ');

        return query.LinqToQuerystring(typeof(T), queryString, forceDynamicProperties, maxPageSize);
    }
}
public class PlantsModule : BaseApiModule
{
    public PlantsModule(IPlantsRepository plantsRepository)
        : base("/plants")
    {
        Get["/"] = _ => plantsRepository.Plants()
                                        .Where(p => !p.IsPrivate)
                                        .LinqToQuerystring(Context.Request.Url);
    }
}

Returning the object directly will force Nancy to return Json.

@beyond-code-github
Copy link
Owner

Hi @shivu333, did the response answer your question or would you like further explanation?

@shivu333 shivu333 closed this as completed Jan 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants