循环数据以在Web api中返回Object的有效方法#

时间:2016-03-11 10:56:20

标签: c# linq api rest

我有数据需要循环并将其转换为嵌套数组的JSOn格式。 以下是我的数据 现在我看到两个选项来完成这个过程

ClientName      Dno     DnoId     Dfull        DfullId
ClientA        AB342A   16711   AB342A-J2015    1
ClientB        AB544A   6648    AB544A-J20131   2
ClientB        AB544A   6648    AB544A-J20151   3
ClientB        AB544A   6648    AB544A-J2015T   4
ClientB        BD944A   6679    BD944A-D20131   5
ClientC        CA778A   12073   CA778A-J20151   6

我想要的输出就像下面的

[{  
    "ClientName":"ClientA",
    "DnoList":[  
      {  
        "DnoId":"16711",
        "Dno":"AB342A",
        "DfullList":[  
          {  
            "DfullId":"1",
            "Dfull":"AB342A-J2015"
          }
        ]
      }
    ]
  },
  {  
    "ClientName":"ClientB"
    "DnoList":[  
      {  
        "DnoId":"6648",
        "Dno":"AB544A",
        "DfullList":[  
          {  
            "DfullId":"2",
            "Dfull":"AB544A-J20131"
          },{  
            "DfullId":"3",
            "Dfull":"AB544A-J20151"
          },{  
            "DfullId":"4",
            "Dfull":"AB544A-J2015T"
          }
        ]
      },
      {  
        "DnoId":"6679",
        "Dno":"BD944A",
        "DfullList":[ {  
            "DfullId":"5",
            "Dfull":"BD944A-D20131"
          } ]
      }
    ]
  }]

现在我找到两种方法来获得这种类型的输出 1.使用嵌套数组创建三个不同的类,然后循环遍历 2.循环通过datatable / list ??

但我没有接近它。

任何人都可以帮助更好的方法。

编辑:我正在使用的Linq查询如下

var objlist = from tblA in context.TableA
                    join tblB in context.TableB on tblA.lng_clientid equals tblB.lng_id
                    where tblA.int_deleted.Equals(0)                              
                    select new Client()
                    {
                        ClientName = tblA.str_client,,
                        DnoId = tblA.lng_dnoid,
                        Dno = tblA.str_dno,
                        Dfull = tblA.str_dfull,
                        DfullId = tblA.lng_id
                    };

和课程如下

public class Client
    {
        public string ClientName { get; set; }
        public int DnoId { get; set; }
        public string Dno { get; set; }
        public string Dfull { get; set; }
        public int DfullId { get; set; }
    }

我有其他方法,如下所示

public class MyData
{
    public string ClientName { get; set; }
    public List<DnoList> DnoLists { get; set; }
}

public class DnoList
{
    public int DnoId { get; set; }
    public string Dno { get; set; }
    public List<DfullList> DfullLists { get; set; }
}

public class DfullList
{
    public int DfullId { get; set; }
    public string Dfull { get; set; }
}

3 个答案:

答案 0 :(得分:1)

您正在寻找的是经典的嵌套分组。

让我们从展示您的数据的平面查询开始(您可以通过将其嵌入到分组查询中来获得所需的输出,但是为了更好的可读性,请单独保留它 - 它不会单独执行):

var clients = 
    from tblA in context.TableA
    join tblB in context.TableB on tblA.lng_clientid equals tblB.lng_id
    where tblA.int_deleted.Equals(0)                              
    select new Client()
    {
        ClientName = tblA.str_client,,
        DnoId = tblA.lng_dnoid,
        Dno = tblA.str_dno,
        Dfull = tblA.str_dfull,
        DfullId = tblA.lng_id
    };

要将其转换为所需的输出格式,您需要首先执行ClientName,然后DnoId, Dno执行嵌套分组,并将结果投影到您创建的自定义类中:

var output =
    (from c in clients
     group c by c.ClientName into clientNameGroup
     select new MyData
     {
         ClientName = clientNameGroup.Key,
         DnoLists = (
             from c in clientNameGroup
             group c by new { c.DnoId, c.Dno } into dnoGroup
             select new DnoList
             {
                 Dno = dnoGroup.Key.Dno,
                 DnoId = dnoGroup.Key.DnoId,
                 DfullLists = (
                     from c in dnoGroup
                     select new DfullList
                     {
                         Dfull = c.Dfull,
                         DfullId = c.DfullId
                     }
                 ).ToList()
             }).ToList()
     }).ToList();

答案 1 :(得分:0)

好吧,假设您的客户列表为:

public class Client {
    public string Name { get; set; }
    public string Dno { get; set; }
    public string DnoId { get; set; }
    public string Dfull { get; set; }
    public string DfullId { get; set; }
}

你可以提取你的结果(效率不高,也不干净):

            List<Client> clients = ... //your list here;

            clients.GroupBy(x => x.Name)
                .Select(
                    x =>
                        new
                        {
                            ClientName = x.Key,
                            DnoList =
                                x.ToList()
                                    .Select(
                                        dn =>
                                            new
                                            {
                                                DnoId = dn.DnoId,
                                                Dno = dn.Dno,
                                                DfullList =
                                                    x.ToList()
                                                        .Select(df => new {DfullId = df.DfullId, Dfull = df.Dfull})
                                            })
                        });

答案 2 :(得分:0)

您需要正确地映射您的类,并创建映射到这些类的类,这些类可以轻松地序列化/反序列化。

public class MyData
{
    public string ClientName { get; set; }
    public List<DnoList> DnoLists { get; set; }
}

public class DnoList
{
    public int DnoId { get; set; }
    public string Dno { get; set; }
    public List<DfullList> DfullLists { get; set; }
}

public class DfullList
{
    public int DfullId { get; set; }
    public string Dfull { get; set; }
}