【问题标题】:Adding array title to Linq-to-SQL-to-JSON in Web API在 Web API 中将数组标题添加到 Linq-to-SQL-to-JSON
【发布时间】:2026-02-07 23:00:01
【问题描述】:

我在 Web API Web 服务中使用 Linq to SQL 从数据库中检索数据并返回 JSON 文件。

我的问题实际上很简单,但是我浏览了论坛并找不到答案。请在下面找到我的问题的描述,以及我的(简化的)源代码。

我返回的对象有两个级别的数据。为了让您理解,这是我的课程的样子:

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; }
}

您可以看到,对于每个 X1 对象,我可以嵌套多个 X2 对象。

为了检索它,我在我的 WebAPI 控制器中使用了以下 Linq to SQL:

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);
}

我的网络服务工作正常并返回以下 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
      }
    ]
  }
]

问题是,要在我的移动应用程序中反序列化它,我必须使用如下类:

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; }
}

但我希望能够使用如下类:

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; }
}

我使用过 JSON 编辑器,并且知道获得此文件的解决方案是使用以下 JSON 文件而不是之前的文件:

{
  "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
        }
      ]
    }
  ]
}

请注意,唯一改变的是我向我的 X 数组(“在线 X”)添加了一个标题。这就是为什么我说我的问题很简单。但问题是,我不知道如何在 Web API 中做到这一点。这只是我的 Linq to SQL 请求中的一个小改动吗?我应该构建一个自定义 JSON 序列化程序吗?

我希望我的问题足够清楚,如果您想了解更多信息,我很乐意提供。

提前非常感谢

编辑:

好的,我找到了解决方案,确实很简单。这里是:

我不得不更换:

return Ok(xs);

通过

return Ok(new { OnlineXs = xs });

【问题讨论】:

  • 数小时挠头后,我终于得到了你的答案。谢谢家伙;)

标签: asp.net json linq


【解决方案1】:

只是为了改写您的答案,当您返回 IHttpActionResult 时,只需将查询结果分配给命名属性并返回如下:

return Ok(new { OnlineXs = xs});

【讨论】: