I have two lists in my program, and I joined them using LINQ query that displays all data from both lists, but I want to display only white and black from both of the lists using join I did this using union and LINQ Projection but That is not I want, I want to do this using LINQ JOIN, here is my solution
What I have tried:
static void Main()
{
List cars = new List
{
new Cars { Make = "Honda", Model = 2000, Color = "Black" },
new Cars { Make = "Suzuki", Model = 1999, Color = "White" },
new Cars { Make = "Toyota", Model = 1988, Color = "Green" },
new Cars { Make = "Kia", Model = 2121, Color = "Blue" }
};
List makeby = new List
{
new MakeBy { Make = "Tesla", Model = 1998, Color = "Black" },
new MakeBy { Make = "Audi", Model = 2015, Color = "White" },
new MakeBy { Make = "Mercedes", Model = 2021, Color = "Green" },
new MakeBy { Make = "Ford", Model = 1991, Color = "Blue" }
};
var CombineCars = cars.Join(makeby,
c => c.Color,
m => m.Color,
(c, m) => new
{
carMake = c.Make,
carModel = c.Model,
carColor = c.Color,
makeByColor = m.Color,
makeByModel = m.Model,
makeByMake = m.Make
});
foreach (var car in CombineCars)
{
Console.WriteLine($"Car model: {car.carColor}, car make: {car.carMake}, Car Color: {car.carModel}, Make By: {car.makeByColor}, Make Color is: {car.makeByMake}, Model is: {car.makeByModel}");
}
Console.ReadLine();
}