c#linq-to-sql EF查询以匹配特定的JSON结构

时间:2016-10-06 15:19:29

标签: c# asp.net json entity-framework linq

我的JSON具有以下结构:

[

  {

    "ID": 1,

    "Label": "Reg Scheme",

    "Colours": [

      {

        "ID": 1,

        "Value": "0x3333cc",

        "Result": 1,

        "Label": null

      },

      {

        "ID": 2,

        "Value": "0x666699",

        "Result": 2,

        "Label": null

      },

      {

        "ID": 3,

        "Value": "0x009966",

        "Result": 3,

        "Label": null

      }

    ]

  },

  {

    "ID": 2,

    "Label": "Spesh Scheme",

    "Colours": [

      {

        "ID": 11,

        "Value": "0x59699c",

        "Result": 1,

        "Label": null

      },

      {

        "ID": 12,

        "Value": "0x0070ff",

        "Result": 2,

        "Label": null

      },

      {

        "ID": 13,

        "Value": "0x90865e",

        "Result": 3,

        "Label": null

      }

    ]

  },

我有一个实体数据集,我加入了所有相关信息,并尝试通过单个linq-to-sql EF查询生成具有该结构的JSON,以返回到webapi方法。

到目前为止,我的查询是:

return
    DbContext.Schemes
            .Join(
                DbContext.SchemeColours,
                s => s.SchemeID,
                sc => sc.SchemeID,
                (s, sc) => new
                    {
                        s.SchemeID,
                        s.Label,
                        sc.Colour,
                        sc.Result,
                        sc.ColourID
                    })
            .Select(a =>
                    new Overlay.ReportColourScheme
                        {
                            ID = a.SchemeID,
                            Label = a.Label,
                            Colours = new List<Overlay.ReportColour>
                                {
                                    new Overlay.ReportColour
                                        {
                                            ID = a.ColourID,
                                            Value = a.Colour,
                                            Result = a.Result
                                        }
                                }
                        })
            .ToArray();

几乎存在但不完全:

[

  {

    "ID": 1,

    "Label": "Regular Scheme",

    "Colours": [

      {

        "ID": 1,

        "Value": "0x3333cc",

        "Result": 1,

        "Label": null

      }

    ]

  },

  {

    "ID": 1,

    "Label": "Regular Scheme",

    "Colours": [

      {

        "ID": 2,

        "Value": "0x666699",

        "Result": 2,

        "Label": null

      }

    ]

  },

  {

    "ID": 1,

    "Label": "Regular Scheme",

    "Colours": [

      {

        "ID": 3,

        "Value": "0x009966",

        "Result": 3,

        "Label": null

      }

    ]

  },

  {

    "ID": 2,

    "Label": "Protanopia adjusted Scheme",

    "Colours": [

      {

        "ID": 11,

        "Value": "0x59699c",

        "Result": 1,

        "Label": null

      }

    ]

  },

  {

    "ID": 2,

    "Label": "Protanopia adjusted Scheme",

    "Colours": [

      {

        "ID": 12,

        "Value": "0x0070ff",

        "Result": 2,

        "Label": null

      }

    ]

  },

  {

    "ID": 2,

    "Label": "Protanopia adjusted Scheme",

    "Colours": [

      {

        "ID": 13,

        "Value": "0x90865e",

        "Result": 3,

        "Label": null

      }

    ]

  },

当然,它会为每个resultID创建一个新列表。顶级ID是一个SchemeID-我正在寻找的逻辑是:“使用特定的schemeID获取前3个结果,将它们添加到Colors中的列表中,然后转到下一个schemeID”< / p>

我相信这会产生与我开始发布的相同的JSON。

非常感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

<search-results #parent>
  <div>{{ parent.item.title }}</div>
  <div>{{ parent.item.id }} </div>
</search-results>

答案 1 :(得分:1)

主要问题是您使用的是Join实际需要Group Join的地方:

return DbContext.Schemes
    .GroupJoin(DbContext.SchemeColours,
        s => s.SchemeID,
        sc => sc.SchemeID,
        (s, colours) => new Overlay.ReportColourScheme
        {
            ID = s.SchemeID,
            Label = s.Label,
            Colours = colours
                .Select(sc => new Overlay.ReportColour
                {
                    ID = sc.ColourID,
                    Value = sc.Colour,
                    Result = sc.Result,
                })
                .ToList()
        })
    .ToArray();

但是,既然您正在使用实体框架,那么如果您定义(如果您已经没有)并使用导航属性,那将会更好更好用:

class Scheme
{
    // ...
    public ICollection<SchemeColour> Colours { get; set; }
}

然后简单地

return DbContext.Schemes
    .Select(s => new Overlay.ReportColourScheme
    {
        ID = s.SchemeID,
        Label = s.Label,
        Colours = s.Colours
            .Select(sc => new Overlay.ReportColour
            {
                ID = sc.ColourID,
                Value = sc.Colour,
                Result = sc.Result,
            })
           .ToList()
    })
    .ToArray();