Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary with lambda expression

Sorry for asking but I just can't seem to find the lambda expression for the next code:

private void SelectIdCeoButton_Click(object sender, EventArgs e)
{
      int id = Convert.ToInt32(this.selectedIdCeo.Text);
      string name = "";

      if (id < 1 || id > 10)
      {
          throw new ArgumentException();
      }
      else
      {
          foreach (var line in ceoExtra.GiveCeoInfo())
          {
              CeoDiscription ceoDis = line.Value;

              if (id.Equals(ceoDis.id))
              {
                  name = ceoDis.ceoName.ToString();
              }
          }
          this.infoBox.Items.Add("chosen ceo: " + name);
      }
  }

It's for the inner foreach code block.

I did my research for trying to use a lambda expression but it gives an error the whole time. It works fine like this but I need to find the lambda expression for it.

I made an enum list of CEO's and if I give the ID in my interface, it needs to return the CEO with that ID. Here are the other classes that I use for it:

class CeoDiscriptionExtra : FactoryClass
{
    CeoDiscription ceo1 = new CeoDiscription(1, CEO.Bill_Gates, 60, 100000000);
    CeoDiscription ceo2 = new CeoDiscription(2, CEO.Dominic_Barton, 54, 6500000);
    CeoDiscription ceo3 = new CeoDiscription(3, CEO.Elon_Musk, 49, 2500000);
    CeoDiscription ceo4 = new CeoDiscription(4, CEO.Jim_Turley, 40, 6000000);
    CeoDiscription ceo5 = new CeoDiscription(5, CEO.Joe_Tucci, 35, 6000000);
    CeoDiscription ceo6 = new CeoDiscription(6, CEO.John_Schlifske, 47, 4500000);
    CeoDiscription ceo7 = new CeoDiscription(7, CEO.Mark_Zuckerberg, 33, 7000000);
    CeoDiscription ceo8 = new CeoDiscription(8, CEO.Paul_Jacobs, 45, 900000);
    CeoDiscription ceo9 = new CeoDiscription(9, CEO.Richard_Davis, 68, 1200000);
    CeoDiscription ceo10 = new CeoDiscription(10, CEO.Tom_Cook, 50, 5000000);

    Dictionary<int, CeoDiscription> dictionaryCeo = new Dictionary<int, CeoDiscription>();

    public void CeoInitialize()
    {
        dictionaryCeo.Add(ceo1.id, ceo1);
        dictionaryCeo.Add(ceo2.id, ceo2);
        dictionaryCeo.Add(ceo3.id, ceo3);
        dictionaryCeo.Add(ceo4.id, ceo4);
        dictionaryCeo.Add(ceo5.id, ceo5);
        dictionaryCeo.Add(ceo6.id, ceo6);
        dictionaryCeo.Add(ceo7.id, ceo7);
        dictionaryCeo.Add(ceo8.id, ceo8);
        dictionaryCeo.Add(ceo9.id, ceo9);
        dictionaryCeo.Add(ceo10.id, ceo10);
    }

    public Dictionary<int, CeoDiscription> GiveCeoInfo()
    {
        foreach (KeyValuePair<int, CeoDiscription> line in dictionaryCeo) //show everything in dictionaryCeo
        {
            CeoDiscription ceoDis = line.Value;
            Console.WriteLine("ID = {0}, Name = {1}, Age = {2}, Salary = {3}", ceoDis.id, ceoDis.ceoName, ceoDis.age, ceoDis.salary);               
        }

        return dictionaryCeo;
    }

    public override string GetCompanyName()
    {
        throw new NotImplementedException();
    }

    public override double GetCost()
    {
        throw new NotImplementedException();
    }

    public override double GetYield()
    {
        throw new NotImplementedException();
    }
}

[Serializable]
class CeoDiscription : FactoryClass
{
    public int id;
    public CEO ceoName;
    public int age;
    public double salary;       

    public CeoDiscription(CEO CeoN)
    {
        this.ceoName = CeoN;
    }

    public CeoDiscription(int id, CEO name, int age, double salary)
    {
        this.id = id;
        this.ceoName = name;
        this.age = age;
        this.salary = salary;
    }

    public CEO GetCeo()
    {
       return ceoName;
    }

    public void SetCeo(CEO CeonS)
    {
        this.ceoName = CeonS;
    }          

    public override string GetCompanyName()
    {
        throw new NotImplementedException();
    }

    public override double GetCost()
    {
        throw new NotImplementedException();
    }

    public override double GetYield()
    {
        throw new NotImplementedException();
    }        
}

[Serializable]
abstract class FactoryClass
{
    Boolean existing = false;
    public FactoryClass()
    {

    }

    public FactoryClass(Boolean existing)
    {
        this.existing = existing;
    }

    public double Cost { get; set; }
    public double Yield { get; set; }
    public string CompanyName { get; set; }

    public abstract double GetYield();
    public abstract double GetCost();
    public abstract string GetCompanyName();

    public enum CEO
    {
        Elon_Musk,
        Tom_Cook,
        Bill_Gates,
        Mark_Zuckerberg,
        Dominic_Barton,
        Jim_Turley,
        John_Schlifske,
        Joe_Tucci,
        Paul_Jacobs,
        Richard_Davis
    }

    public static MemoryStream SerializeToStream(object o)
    {
        MemoryStream stream = new MemoryStream();
        IFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, o);
        return stream;
    }

    public static object DeserializeFromStream(MemoryStream stream)
    {
        IFormatter formatter = new BinaryFormatter();
        stream.Seek(0, SeekOrigin.Begin);
        object o = formatter.Deserialize(stream);
        return o;
    }
}
like image 983
Vengenecx Avatar asked Jan 28 '23 02:01

Vengenecx


1 Answers

If you are looking for the name of an item identified by its ID, and you know that the ID is unique, you could do this:

var name = ceoExtra.GiveCeoInfo().Values
    .FirstOrDefault(ceoDis => ceoDis.id == id)?.ceoName?.ToString() ?? "";

Note that this is not the exact equivalent of your code, because it picks the first match, while your code picks the last one. If the match is unique, it makes no difference.

like image 173
Sergey Kalinichenko Avatar answered Jan 30 '23 23:01

Sergey Kalinichenko