3

I am using Linq to SQL in a Web API web service to retrieve data from a database and return a JSON file.

My question is actually pretty simple, but I have been through the forums and couldn't find an answer. Please find below the description of my issue, as well as my (simplified) sourcecode.

The objects I return have two levels of data. To make you understand, here is how my classes look like :

public class X
{
    public int ID { get; set; }
    public DateTime date { get; set; }

    public virtual ICollection<Y> Ys
    public virtual ApplicationUser User { get; set; }
}

public class Y
{
    public int ID { get; set; }
    public int XID { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }

    public virtual X x { get; set; }
}

You can see that for each X1 object, I can have several X2 objects nested.

To retrieve that, I use the following Linq to SQL in my WebAPI Controller :

public IHttpActionResult GetXsByUser(string userID)
    {
        var xs = (from x in db.Xs
                        where x.User.Id == userID
                        orderby x.date
                        select new
                        {
                            x_id = x.ID,
                            date = x.date,
                            Ys = (from y in db.Ys
                                         where x.User.Id == userID && x1.ID == y.XID
                                         select new
                                         {
                                             unit_price = y.Price,
                                             quantity = y.Quantity
                                         })
                        });
        if (xs == null)
        {
            return NotFound();
        }
        return Ok(xs);
}

My web service works fine and returns the following JSON :

[
  {
    "$id": "1",
    "x_id": 1,
    "date": "2014-01-24T00:00:00",
    "Ys": [
      {
        "$id": "2",
        "unit_price": 2.47,
        "quantity": 2
      },
      {
        "$id": "3",
        "unit_price": 1.25,
        "quantity": 3
      },
      {
        "$id": "4",
        "unit_price": 1.99,
        "quantity": 2
      }
    ]
  },
  {
    "$id": "5",
    "x_id": 2,
    "date": "2014-01-28T00:00:00",
    "Ys": [
      {
        "$id": "6",
        "unit_price": 6.22,
        "quantity": 1
      },
      {
        "$id": "7",
        "unit_price": 1.2,
        "quantity": 3
      }
    ]
  }
]

The problem is, to then deserialize this in my mobile app, I have to use classes as follows :

public class Y
{
    public string _$id { get; set; }
    public double unit_price { get; set; }
    public int quantity { get; set; }
}

public class RootObject
{
    public string _$id { get; set; }
    public int x_id { get; set; }
    public string date { get; set; }
    public List<Y> Ys { get; set; }
}

But i would like to be able to use classes as follow :

public class Y
{
    public string _$id { get; set; }
    public double unit_price { get; set; }
    public int quantity { get; set; }
}

public class OnlineX
{
    public string _$id { get; set; }
    public int x_id { get; set; }
    public string date { get; set; }
    public List<Y> Ys { get; set; }
}

public class RootObject
{
    public List<OnlineX> OnlineXs { get; set; }
}

I have worked with a JSON editor and know that the solution to get this is to have the following JSON file instead of the previous one :

{
  "OnlineXs": [
    {
      "$id": "1",
      "x_id": 1,
      "date": "2014-01-24T00:00:00",
      "Ys": [
        {
          "$id": "2",
          "unit_price": 2.47,
          "quantity": 2
        },
        {
          "$id": "3",
          "unit_price": 1.25,
          "quantity": 3
        },
        {
          "$id": "4",
          "unit_price": 1.99,
          "quantity": 2
        }
      ]
    },
    {
      "$id": "5",
      "x_id": 2,
      "date": "2014-01-28T00:00:00",
      "Ys": [
        {
          "$id": "6",
          "unit_price": 6.22,
          "quantity": 1
        },
        {
          "$id": "7",
          "unit_price": 1.2,
          "quantity": 3
        }
      ]
    }
  ]
}

Notice that the only thing that changes is that I add a title to my array of Xs ("Online Xs"). That is why I said that my question is simple. But the thing is, I have no idea how to do that in Web API. Is it just a small change in my Linq to SQL request? Should i build a custom JSON serializer?

I hope that my question is clear enough, and if you want some more information, I'll be happy to provide them.

Thanks a lot in advance

EDIT :

Ok, I've found the solution, it was simple indeed. Here it is :

I had to replace :

return Ok(xs);

by

return Ok(new { OnlineXs = xs });
1
  • After hours scratching my head I finally ended up your answer. thanks guy ;) Commented Nov 14, 2014 at 4:03

1 Answer 1

2

Just to rephrase your answer, when you return the IHttpActionResult, just assign the query result to a named property and return it like:

return Ok(new { OnlineXs = xs});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.