Created
July 25, 2023 14:24
-
-
Save junaid109/c894bcf63ff652c9dd0715daeff74a1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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