What you're looking for is a Cartesian Product. I'm going to define two small objects so that I can show a concrete LINQ query. For convenience I'll make the package type an enum
, but in reality it doesn't matter so long as the objects representing a factory and component have a common field of the same type.
private enum PackagingType
{
Type1
};
private class Factory
{
public string Name { get; set; }
public PackagingType Type { get; set; }
}
private class Component
{
public string Name { get; set; }
public PackagingType Type { get; set; }
}
var factories = new List<Factory>
{
new Factory {Name = "A", Type = PackagingType.Type1},
new Factory {Name = "B", Type = PackagingType.Type1},
new Factory {Name = "C", Type = PackagingType.Type1}
};
var components = new List<Component>
{
new Component {Name = "1", Type = PackagingType.Type1},
new Component {Name = "2", Type = PackagingType.Type1},
new Component {Name = "3", Type = PackagingType.Type1},
new Component {Name = "4", Type = PackagingType.Type1}
};
Then we can join factories
to components
on the Type
field using the LINQ extension method Join
, which returns a Cartesian Product for us. That looks like:
var cartesianProduct = factories.Join(components,
factory => factory.Type,
component => component.Type,
(factory, component) =>
new
{
Type = factory.Type,
FactoryName = factory.Name,
ComponentName = component.Name
});
This results in the output:
Type1 A 1
Type1 A 2
Type1 A 3
Type1 A 4
Type1 B 1
Type1 B 2
Type1 B 3
Type1 B 4
Type1 C 1
Type1 C 2
Type1 C 3
Type1 C 4
If you had a third object with a Many to Many relationship based on Packaging then you could simply join that list of objects with the current Cartesian Product on its Type
field to get a Cartesian Product across all three objects. See Eric Lippert's answer here for more information.