Skip to content

Instantly share code, notes, and snippets.

@junaid109
Created July 25, 2023 14:24
Show Gist options
  • Save junaid109/c894bcf63ff652c9dd0715daeff74a1c to your computer and use it in GitHub Desktop.
Save junaid109/c894bcf63ff652c9dd0715daeff74a1c to your computer and use it in GitHub Desktop.
List<Device> childDevices = new List<Device>
{
new Device { Id = 1 },
};
List<Device> parentDevices = new List<Device>
{
new Device { Id = 1 }, // Parent device with ID 1
new Device { Id = 4 } // Parent device with ID 4
};
// Find matches between childDevices and parentDevices based on the ID
var matches = childDevices.Join(
parentDevices,
childDevice => childDevice.Id,
parentDevice => parentDevice.Id,
(childDevice, parentDevice) => new { ChildDevice = childDevice, ParentDevice = parentDevice }
);
// Parent the child devices to their corresponding parent devices
foreach (var match in matches)
{
// Assuming your Device class has a property to store the parent device
match.ChildDevice.ParentDevice = match.ParentDevice;
}
// Now you can access the parent device
foreach (var childDevice in childDevices)
{
if (childDevice.ParentDevice != null)
{
Console.WriteLine($"Child Device ID: {childDevice.Id}, Parent Device ID: {childDevice.ParentDevice.Id}");
}
else
{
Console.WriteLine($"Child Device ID: {childDevice.Id}, No Parent Device");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment