using System.Collections.Generic;
public static void Main()
List<MachineStatus> machines = new List<MachineStatus>
new MachineStatus { MachineName = "Machine1", Department = "Department1", LocationName = "Loc1",
Devices = new List<DeviceStatus>
new DeviceStatus { DeviceName = "Device A", IpAddress = "123", DeviceType = "Foo", Status = ConnectionStatus.Connected },
new DeviceStatus { DeviceName = "Device B", IpAddress = "345", DeviceType = "Bar", Status = ConnectionStatus.Disconnected },
new DeviceStatus { DeviceName = "Device C", IpAddress = "257", DeviceType = "Foo", Status = ConnectionStatus.Disconnected }
new MachineStatus { MachineName = "Machine2", Department = "Department2", LocationName = "Loc3", Devices = new List<DeviceStatus>
new DeviceStatus { DeviceName = "Device C", IpAddress = "764", DeviceType = "Foo", Status = ConnectionStatus.Connected },
new DeviceStatus { DeviceName = "Device D", IpAddress = "904", DeviceType = "Zed", Status = ConnectionStatus.Connected }
var result = machines.SelectMany(x => x.Devices, (machineObj, devices) => new { machineObj, devices })
.GroupBy(x => new { x.devices.DeviceType, x.machineObj.Department, x.machineObj.LocationName })
DeviceType = x.Key.DeviceType,
Department = x.Key.Department,
Location = x.Key.LocationName,
foreach (var item in result)
Console.WriteLine("Device Type: {0}", item.DeviceType);
Console.WriteLine("Department : {0}", item.Department);
Console.WriteLine("Location: {0}", item.Location);
foreach (var machine in item.machines)
Console.WriteLine("Machine : {0}, DeviceName : {1}", machine.machineObj.MachineName, machine.devices.DeviceName);
Console.WriteLine("------------------------------------------");
public class MachineStatus
public string MachineName { get; set; }
public string Department { get; set; }
public string LocationName { get; set; }
public IEnumerable<DeviceStatus> Devices { get; set; }
public class DeviceStatus
public string DeviceName { get; set; }
public string DeviceType { get; set; }
public string IpAddress { get; set; }
public ConnectionStatus Status { get; set; }
public enum ConnectionStatus