1

So, lets say I have a list of cars. Each item in the list has a brand and a color property. I would like to find out for each brand how many there are of the same color, and then print that info. The list will be filled by user input. There's no way for me to tell which exact values will be in the list.

Example:

class Car
    {
        public string brand;
        public string color;
    }

    private void DoSomething()
    {
        List<Car> cars = new List<Car>();
        cars.Add(new Car { brand = "Toyota", color = "blue" });
        cars.Add(new Car { brand = "Toyota", color = "red" });
        cars.Add(new Car { brand = "Toyota", color = "blue" });
        cars.Add(new Car { brand = "Audi", color = "red" });
        cars.Add(new Car { brand = "Audi", color = "red" });
        cars.Add(new Car { brand = "Audi", color = "blue" });

        // Find out for each brand how many there are of the same color, and then print that info
        // Example output: Toyota: 2 blue, 1 red
        //                 Audi:   2 red, 1 blue
    }

I've spend a long time searching for a way to do this. All I was able to figure out was how to get the number of occurences of a certain item in the list.

If my question is unclear please let me know, I will try to explain a bit more.

2
  • 3
    You could use LINQ to do this. If you aren't familiar with LINQ, it'll require a bit of research, maybe some headache at first. But you'll end up with fairly simple code. Commented Jul 5, 2017 at 16:00
  • 1
    Show what you´ve tried, in particular that code. Commented Jul 5, 2017 at 16:00

3 Answers 3

4

Some linq should do this:

var result = cars.GroupBy(x => new { x.brand, x.color })
    .Select(x => new { 
        Brand = x.Key.brand, 
        Color = x.Key.color, 
        Count = x.Count() 
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked for me. All I had to do after this was iterate over the results with a loop. I'll definately have to learn some more about LINQ!
3
var carGroups = cars.GroupBy(c => new { c.brand, c.color });
foreach (var carGroup in carGroups)
{
    Console.WriteLine($"{carGroup.Key}, count: {carGroup.Count()}");
}

2 Comments

Thank you for your answer! This could work for me, however the accepted answer gives me better results in my case.
You're welcome. No problem at all, you should accept the answer that suits the question most. If it's HimBromBeere's answer, then he deserves the reputation.
1
  var grp = cars.GroupBy(g => new { g.brand, g.color }).Select(n => new {
                brand = n.Key.brand,
                color = n.Key.color,
                count = n.Count()

            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.