To show what I want to achieve, I prepared a simplified example:
Let's say I have an object like this:
public class SoldProduct
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
Then, somewhere I have a method which gets different lists of SoldProduct (from different categories) like this:
List<SoldProduct> toolsSold = GetToolSales();
List<SoldProduct> materialsSold = GetMaterialSales();
List<SoldProduct> foodsSold = GetFoodSales();
Now, I need to merge them into one list of rows, where a row is something like this:
public class SoldProductRow
{
public SoldProduct ToolSold { get; set; }
public SoldProduct MaterialSold { get; set; }
public SoldProduct FoodSold { get; set; }
}
Question:
So, how to convert these 3 lists into a List<SoldProductRow>? Is there some simple, efficient LINQ query? Maybe there is a really simple and efficient way to write it manually?
Let's imagine this row is something like an Excel row; we want to have 2 columns for name and date of tool sales, 2 columns for name and date of material sales, and 2 columns for name and date of food sales. Essentially list them side by side.
Difficulties:
Lists can be of different length, so if we have 3 elements in toolsSold but 5 in materialsSold and foodsSold, then the rows list should have 5 elements (longest list length) where the first 3 elements of the rows list will have all properties, but the 4th and 5th elements should have null in ToolSold and proper values in MaterialSold and FoodSold
List<SoldProduct> length is usually between 1 and 3, but actually there is List<Parent> with possibly hundreds of objects and every parent has 5 List<SoldProduct> so, I need to create hundreds of rows.
Example:
If I had lists:
toolsSold: [
{Name: hammer, Date:24-09-2020}
]
materialsSold: [
{Name: wood, Date:23-09-2020},
{Name: steel, Date:17-08-2020}
]
foodsSold: [
{Name: apple, Date:07-02-2020}
]
I would want this result (2 rows because the longest list has 2 entities):
SoldProductsRows: [
{
ToolSold: {Name: hammer, Date:24-09-2020}
MaterialSold: {Name: wood, Date:23-09-2020}
FoodSold: {Name: apple, Date:07-02-2020}
},
{
ToolSold: null
MaterialSold: {Name: steel, Date:17-08-2020}
FoodSold: null
},
]