【问题标题】:Efficient way to loop through data to return Object in Web api c#在Web api c#中循环数据以返回对象的有效方法
【发布时间】:2016-03-11 10:56:20
【问题描述】:

我有数据需要循环遍历并将其转换为带有嵌套数组的 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; }
}

【问题讨论】:

  • 你可以循环遍历表格,形成类似的数据结构并序列化。
  • @Mahajan344 你想要什么?为上述场景定义最佳类结构......或者如何使用一个类场景??
  • 请更新您的 LINQ 查询以包含 DnoId

标签: c# linq api rest


【解决方案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();

【讨论】:

  • 感谢您的回复。我有一个问题,如果我在 Mydata 类中再多一列说 ClientId 和 Clientname 怎么办。那么我如何在以后的查询中选择 clientid ?我尝试使用 clientNameGroup.Key.Clientid 但它给出错误
  • 有没有其他方法可以选择该列
  • 您需要将它包含在第一个分组中,即您将使用 by new { c.ClientId, c.ClientName } 而不是 by c.ClientName,然后使用 select new MyData { ClientId = clientNameGroup.Key.ClientId, ClientName = clientNameGroup.Key.ClientName, ... (the rest)。如果您通过在基本查询中包含该列来更新您的问题,我将更新答案。
  • 我明白了.. 非常感谢您的帮助 :)
【解决方案2】:

好吧,鉴于您的客户列表如下:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 我有类似的类结构,但如何查询和填写适当的方式?
    • 你有什么数据库,你用的是实体框架之类的ORM吗?
    • 是的,我正在使用 linq (DBML)
    • 请在您当前从数据库中选择数据的位置添加代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多