我对多个数据有一些问题。
我会在这里添加我的样本。
foreach (HotelPaxRoom c in h.PaxRoomSearchResults)
{
int index = c.index;
foreach (RoomCategory a in c.RoomCategories)
{
HRoom room = new HRoom();
room.id = a.id;
room.category = a.category;
room.price = a.price;
room.index = c.index;
}
}
实际上PaxRoomSearchResults有数据列表。我将调用PaxRoomSearchResults作为A 在PaxRoomSearchResults里面有RoomCategories List,我将把RoomCategories称为B
我想使用这种逻辑读取数据。
A [0]与A [1]
的例子A [0]对A [1]对A [2]
的例子就像它明智一样.......... 实际上我不能用我的foreach来做到这一点。有没有更好的,更精确/快速的方法呢?
这总是应该用PaxRoomSearchResults第0个索引映射。然后是2,然后2就像明智一样。
请问您是否有任何问题重新解决我的问题。
实际上这个输出是我在xml反序列化后得到的。所以我会添加一小部分内容。这会让你有更好的理解
<PaxRoomSearchResults>
<PaxRoom RoomIndex="1">
<RoomCategories>
<RoomCategory Id="xxx:rrrr:vvv:S7519:8456:115266">
</RoomCategory>
</RoomCategories>
</PaxRoom>
<PaxRoom RoomIndex="2">
<RoomCategories>
<RoomCategory Id="xxx:rrrr:vvv:S7519:8456:33689">
</RoomCategory>
<RoomCategory Id="xxx:rrrr:vvv:S7519:8456:33707">
</RoomCategory>
<RoomCategory Id="xxx:rrrr:vvv:S7519:8456:33695">
</RoomCategory>
<RoomCategory Id="xxx:rrrr:vvv:S7519:8456:115266">
</RoomCategory>
</RoomCategories>
</PaxRoom>
结果应该是这样的
我有这样的separete模型。Public RoomGroup Class
{
public int id {get;set;}
public List<RoomDetails> roomdetails{get;set;}
}
Public RoomDetails Class
{
public string roomcategoryID{get;set;}
public decimal roomPrice{get;set;}
}
输出就像这样
RoomGroup 1 roomcategoryID 001:AMB1:7736:S7519:8456:115266 roomcategoryID 001:AMB1:7736:S7519:8456:33689
RoomGroup 2 roomcategoryID 001:AMB1:7736:S7519:8456:115266 roomcategoryID 001:AMB1:7736:S7519:8456:33707
RoomGroup 3 roomcategoryID 001:AMB1:7736:S7519:8456:115266 roomcategoryID 001:AMB1:7736:S7519:8456:33695
RoomGroup 4 roomcategoryID 001:AMB1:7736:S7519:8456:115266 roomcategoryID 001:AMB1:7736:S7519:8456:115266
答案 0 :(得分:0)
不完全确定你想要什么。也许这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<RoomGroup> roomGroup = new List<RoomGroup>() {
new RoomGroup() {
id = 1,
roomdetails = new List<RoomDetails>() {
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:115266"
},
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:33689"
},
}
},
new RoomGroup() {
id = 2,
roomdetails = new List<RoomDetails>() {
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:115266"
},
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:33707"
},
}
},
new RoomGroup() {
id = 3,
roomdetails = new List<RoomDetails>() {
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:115266"
},
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:33695"
}
}
},
new RoomGroup() {
id = 4,
roomdetails = new List<RoomDetails>() {
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:115266"
},
new RoomDetails() {
roomcategoryID = "001:AMB1:7736:S7519:8456:115266"
}
}
}
};
foreach (RoomGroup group in roomGroup)
{
string output = string.Format("RoomGroup {0} {1}",
group.id.ToString(),
string.Join(" ", group.roomdetails.Select(x => "roomCategoryID " + x.roomcategoryID).ToArray())
);
Console.WriteLine(output);
Console.WriteLine();
}
Console.ReadLine();
}
}
public class RoomGroup
{
public int id {get;set;}
public List<RoomDetails> roomdetails{get;set;}
}
public class RoomDetails
{
public string roomcategoryID{get;set;}
public decimal roomPrice{get;set;}
}
}