need to group data based on `InstanceData` Name property

  • Thread starter Thread starter Yazdani ISTS
  • Start date Start date
Y

Yazdani ISTS

Guest
Hello,

I have one Packet like below,

var dataPacket = new Packet
{
Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
Results = new Result
{
ResultName = "ResultName1",
Instances = new List<Instance>
{
new Instance
{
InstanceName = "InstanceName1",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V1"},
new InstanceData{Name = "N2", Value = "V2"}
}
},
new Instance
{
InstanceName = "InstanceName2",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V3"},
new InstanceData{Name = "N2", Value = "V4"}
}
}
}
}
};

Here are the class structures,

public class Packet
{
public Guid Id { get; set; }
public Result Results { get; set; }
}

public class Result
{
public string ResultName { get; set; }
public List<Instance> Instances { get; set; }
}

public class Instance
{
public string InstanceName { get; set; }
public List<InstanceData> InstanceDatas { get; set; }
}

public class InstanceData
{
public string Name { get; set; }
public string Value { get; set; }
}


For above Packet I want to spilt this into 2 Packets based on InstanceData common Name

  • All N1 from InstanceName1 and InstanceName2 into one packet
  • All N2 from InstanceName1 and InstanceName2 into one packet

Packet1 should be like this,

var packet1 = new Packet
{
Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
Results = new Result
{
ResultName = "ResultName1",
Instances = new List<Instance>
{
new Instance
{
InstanceName = "InstanceName1",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V1"},
}
},
new Instance
{
InstanceName = "InstanceName2",
InstanceDatas = new List<InstanceData>
{
new InstanceData{Name = "N1", Value = "V3"},
}
}
}
}
};


and similarly packet2.

I have tried below, but this will split on InstanceData as well and giving 4 packets. What could be the solution?

var packets = dataPacket.Results
.Instances
.SelectMany(x =>
x.InstanceDatas.Select(y => new Packet()
{
Id = dataPacket.Id,
Results = new Result()
{
ResultName = dataPacket.Results.ResultName,
Instances = new List<Instance>()
{
new Instance()
{
InstanceDatas = new List<InstanceData>() {y},
InstanceName = x.InstanceName
}
}
}
}));

Continue reading...
 
Back
Top