繁体   English   中英

与lambda表达的字典

[英]Dictionary with lambda expression

很抱歉要问,但我似乎无法找到下一个代码的lambda表达式:

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);
      }
  }

这是内部foreach代码块。

我尝试使用lambda表达式进行了研究,但它总是给出错误。 它工作正常,但我需要找到它的lambda表达式。

我制作了一份关于CEO的枚举列表,如果我在我的界面中提供ID ,它需要返回带有该IDCEO 以下是我使用的其他类:

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;
    }
}

如果您要查找由其ID标识的项目的名称,并且您知道该ID是唯一的,则可以执行以下操作:

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

请注意,这不是您的代码的完全等价物,因为它会选择第一个匹配项,而您的代码会选择最后一个匹配项。 如果匹配是唯一的,则没有区别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM