Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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
                               });

           //var uniqueMakeby = makeby.Select(s => s.Colour).Distinct();
           //var selectedCars = cars
           //    .Where(w => uniqueMakeby.Contains(w.Color))
           //    .Select(s => new
           //    {
           //        s.Make,
           //        s.Model,
           //        s.Color,
           //        Country = string.Empty
           //    });
           //var unioned = selectedCars.Union(
           //    from x in makeby
           //    select new
           //    {
           //        Make = string.Empty,
           //        Model = 0,
           //        Color = x.Colour,
           //        x.Country
           //    }
           //);

           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();

       }
Posted
Updated 26-Mar-21 0:03am

1 solution

If I've understood your question correctly, you simply want to filter the joined list:
C#
var combinedCars = from c in cars
                   where c.Color == "White" || c.Color == "Black"
                   join m in makeby on c.Color equals m.Color
                   select new
                   {
                       carMake = c.Make,
                       carModel = c.Model,
                       carColor = c.Color,
                       makeByColor = m.Color,
                       makeByModel = m.Model,
                       makeByMake = m.Make
                   };
Honda  2000 Black Black 1998 Tesla 
Suzuki 1999 White White 2015 Audi 
 
Share this answer
 
Comments
Adi Mirza 26-Mar-21 6:20am    
yup @Richar thanks for the solution. Stay blessed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900