有效的方式来通过数据回路中的Web API C#返回对象 [英] Efficient way to loop through data to return Object in Web api c#
问题描述
我有我需要遍历和嵌套数组转换它JSON格式的数据。
下面是我的数据
现在,我看到两个选项完成这个过程。
CLIENTNAME DNO DnoId Dfull DfullId
客户端A 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:客户端A,
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.遍历数据表/列表?
但我没有得到接近它。
谁能帮助与更好的办法。
编辑:我使用LINQ查询,如下
VAR objlist =从tblA在context.TableA
加入TBLB在context.TableB上tblA.lng_clientid等于tblB.lng_id
其中,tblA.int_deleted.Equals(0)
选择新的客户端()
{
CLIENTNAME = tblA.str_client ,,
DnoId = tblA.lng_dnoid,
DNO = tblA.str_dno,
Dfull = tblA.str_dfull,
DfullId = tblA.lng_id
};
和类,如下
公共类客户端
{
公共字符串CLIENTNAME {搞定;组; }
公众诠释DnoId {搞定;组; }
公共字符串DNO {搞定;组; }
公共字符串Dfull {搞定;组; }
公众诠释DfullId {搞定;组; }
}
另一种方法我已经是不同的类,如下
公共类迈德特
{
公共字符串CLIENTNAME {搞定;组; }
公开名单< DnoList> DnoLists {搞定;组; }
}公共类DnoList
{
公众诠释DnoId {搞定;组; }
公共字符串DNO {搞定;组; }
公开名单< DfullList> DfullLists {搞定;组; }
}公共类DfullList
{
公众诠释DfullId {搞定;组; }
公共字符串Dfull {搞定;组; }
}
你所寻求是一个经典的嵌套分组。
让启动了一台查询presenting您的数据(你可以通过将其嵌入分组查询得到需要的结果,而是让单独保持可读性更好 - 它不会单独执行):
VAR的客户=
从tblA在context.TableA
加入TBLB在context.TableB上tblA.lng_clientid等于tblB.lng_id
其中,tblA.int_deleted.Equals(0)
选择新的客户端()
{
CLIENTNAME = tblA.str_client ,,
DnoId = tblA.lng_dnoid,
DNO = tblA.str_dno,
Dfull = tblA.str_dfull,
DfullId = tblA.lng_id
};
要其转换为所需的输出格式,你需要进行嵌套的分组,首先由 CLIENTNAME
,然后通过 DnoId,DNO
和项目的成果转化为你创建一个自定义类:
VAR输出=
(从客户Ç
C组由c.ClientName到clientNameGroup
选择新迈德特
{
CLIENTNAME = clientNameGroup.Key,
DnoLists =(
从clientNameGroupÇ
通过新的{c.DnoId,c.Dno}成dnoGroup c组
选择新DnoList
{
DNO = dnoGroup.Key.Dno,
DnoId = dnoGroup.Key.DnoId,
DfullLists =(
从dnoGroupÇ
选择新DfullList
{
Dfull = c.Dfull,
DfullId = c.DfullId
}
).ToList()
})。了ToList()
})了ToList()。
I have data that I need to to loop through and convert it JSOn format with nested array. Below is my data Now I see two option to do this process
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
And Output I want is like below
[{
"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"
} ]
}
]
}]
Now I find two approach to get this type of output 1. Create three different class with nested array then loop through that 2. loop through datatable/list ??
But I am not getting close to it.
Can anyone help with with better approach.
EDIT : Linq query I am using is as below
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
};
and class as below
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; }
}
Other Approach I have is different class as below
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; }
}
What you are seeking for is a classical nested grouping.
Let start with a flat query presenting your data (you can get the desired output by embedding it into the grouping query, but let keep it separately for better readability - it will not be executed separately):
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
};
To transform it to the desired output format, you need to perform nested grouping, first by ClientName
, then by DnoId, Dno
, and project the results into a custom classes you've created:
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();
这篇关于有效的方式来通过数据回路中的Web API C#返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!