0

I have a List<Animal> , which contains 100s of objects. There'll be a field which is AnimalType that will be common. I need to fetch this value.

            List<Animal> foundAnimal = db.Animals
                .Where(a => a.Name == "Elephant"
                    )).ToList();

The foundAnimal object has a element called AnimalType (Which is an int) which is the same for all "Elephants". I need to retrieve this value from the above List.

How can I do that ?

Animal object

private string Name {get;set}
private int Age{get;set}
private int AnimalType{get;set}

What I tried:

var ani = foundAnimal.Distinct();

But the above gives me a list of Distinct Animal objects. What I need to know is the value that is common in the Animal Object (for the name Elephant) which is a int value.

UPDATE

new Animal {Name = "Elephant" , Age 12 , AnimalType = 1 } 
new Animal {Name = "Elephant" , Age 12 , AnimalType = 1 } 
new Animal {Name = "Elephant" , Age 12 , AnimalType = 1 } 
new Animal {Name = "Ant" , Age 12 , AnimalType = 2 } 
new Animal {Name = "Ant" , Age 12 , AnimalType = 2 } 
new Animal {Name = "Elephant" , Age 12 , AnimalType = 1 } 
new Animal {Name = "Elephant" , Age 12 , AnimalType = 1 } 
new Animal {Name = "Elephant" , Age 12 , AnimalType = 1 } 

The list will give me Animals where Name is Elephant

List<Animal> foundAnimal = db.Animals
            .Where(a => a.Name == "Elephant"
                )).ToList();

The above Will return Only Objects where the Name is equal to Elephant.

All what I need is to get the AnimalType which is 1 when Name == Elephant.

Hope I was able to explain this clearly.

Illep
  • 16,375
  • 46
  • 171
  • 302

0 Answers0