blaces
blaces

Reputation: 495

How get an prop of Object key in a Dictionary C#?

I have this homework, I have only 1 problem, and I don't know the solution. We have this class, and we musn't create another variables or methods...

I have a beers dictionary with < Beer object, int income >. But the method has got only the Beer object's name (prop), not the object.

And I don't have another idea, how can I get the Beer object's name from a Dictionary

I have only 2 idea, but these don't work.

The first is I tried use a ContainsKey() method. The second is an foreach iteration

using System;
using System.Collections.Generic;

namespace PubBeer
{
    public class Beer
    {
        string name;
        int price; 
        double alcohol;


        public string Name{ get { return name; } }

        public int Price{ get; set; }

        public double Alcohol{ get { return alcohol;} }

        public Sör(string name, int price, double alcohol)
        {
            this.name= name;
            this.price= price;
            this.alcohol= alcohol;
        }


        public override bool Equals(object obj)
        {
            if (obj is Beer)
            {
                Beer other = (Beer)obj;
                return this.name== other.name;
            }
            return false;
        }
    }

    public class Pub
    {

        int income;

        IDictionary<Beer, int> beers= new Dictionary<Beer, int>();


        public int Income{ get; set; }


        public int Sold(string beerName, int mug)
        {
           // Here the problem

            beers; // Here I want to like this: beers.Contains(beerName)
                  //  beers.ContainsKey(Object.Name==beerName) or someone like this 

           // foreach (var item in beers)
           // {
           //     item.Key.Name== beerName;
           //  }


        }
...

Upvotes: 0

Views: 125

Answers (3)

myermian
myermian

Reputation: 32515

Use LINQ to query over the collection of Keys.

//Throws an error if none or more than one object has the same name.
var beer = beers.Keys.Single(b => b.Name == beerName);

beers[beer] = ...;

// -or -

//Selects the first of many objects that have the same name.
//Exception if there aren't any matches.
var beer = beers.Keys.First(b => b.Name == beerName);

beers[beer] = ...;

// -or -

//Selects the first or default of many objects.
var beer = beers.Keys.FirstOrDefault(b => b.Name == beerName);

//You'll need to null check
if (beer != null)
{
    beers[beer] = ...;
}

// etc...

Update: NON-LINQ Alternative

Beer myBeer;

foreach (var beer in beers.Keys)
{
    if (beer.Name == beerName)
    {
        myBeer = beer;
        break;
    }
}

if (myBeer != null)
{
    beers[myBeer] = ...;
}

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160912

You could use Any() on the Keys collection:

if (beers.Keys.Any(x => x.Name == beerName))
{
}

In the worst case this would have to look through all beers though - if you usually look up beers by name you should consider making the beer name the key and the beer object itself the value in the dictionary.

Once you have identified that such a beer exists you can use First() to select it:

 Beer myBeer = beers.First(x => x.Key.Name == beerName).Key;

Upvotes: 1

Andriy Khrystyanovich
Andriy Khrystyanovich

Reputation: 1452

try to use Keys property

beers.Keys.Where(p => p.name == beername ) 

or

beers.Keys.FirstOrDefault(p => p.name == beername)

Upvotes: 0

Related Questions