I have some data that I would like to count and group by. It looks like this:
Cat Cat Dog Dog Dog
My goal is to get a list of objects that have the name of the animal and the number of times it appears in the data set
Object 1
Name: Cat
NumberAppearances: 2
Object 2
Name: Dog
NumberAppearances: 3
I am trying to do this with a LINQ query and this is what I have but the count is wrong. I think it's counting the length instead of the number of times it appears. How would I modify this?
animalData.Select(x => x.AnimalType).Distinct().ToList().ForEach(a =>
AnimalObject animal = new AnimalObject();
animal.Name = a.Name;
animal.Number = a.Distinct().Count();
animalList.Add(animal);
});
animalData.Select(x => x.AnimalType).GroupBy(...). When you're going to use LINQ first do some exercises with allEnumerableextension methods to get them into your system. - Gert ArnoldAnimalType? - Eric Lippert