【问题标题】:How to get value from IEnumerable collection using its Key?如何使用其键从 IEnumerable 集合中获取价值?
【发布时间】:2015-11-17 12:31:42
【问题描述】:

我有如下的 IEnumerable 集合

IEnumerable<Customer> items = new Customer[] 
{ 
     new Customer { Name = "test1", Id = 999 }, 
     new Customer { Name = "test2", Id = 989 } 
};

我想使用密钥Id 获得价值

我尝试如下

public int GetValue(IEnumerable<T> items,string propertyName)
{
      for (int i = 0; i < items.Count(); i++)
      {
           (typeof(T).GetType().GetProperty(propertyName).GetValue(typeof(T), null));
           // I will pass propertyName as Id and want all Id propperty values 
           // from items collection one by one.
      }
}

【问题讨论】:

  • 你为什么不直接使用 Select 方法呢?它也是类型安全的。
  • items.Select(x=&gt;x.Id) 会这样做。你想达到什么目标?
  • 如果您想以“通用”方式执行此操作,您可以为您的方法提供一个 Func 选择器(谓词)而不是您的 propertyName 参数,然后在linq where 子句。与您需要选择的内容相同。如果是整个 T 项目,则不需要特定的 select,但如果只需要特定的属性,则传递另一个选择器函数(Func&lt;T, whatever type&gt; 类型)

标签: c# ienumerable ienumerator


【解决方案1】:
private TextBox [] Collectionstextboxonpanel(Panel panel)
{

    var textBoxspanel1 = panel.Controls.OfType<TextBox>(); // select controls on panle1 by type

    IEnumerable<TextBox> textBoxes = textBoxspanel1; // create collection if need 
    TextBox[] textBoxes1 = textBoxes.ToArray(); // Array collection
    return textBoxes1;                         // get back TextBox Collection
}

【讨论】:

  • 你好伊戈尔!用一两句话解释你在更容易理解的代码中做了什么。
  • @Pluto 的观点始终是一个很好的做法,但对于这些已确定答案的老问题,它尤其很重要。为什么要采用这种方法而不是其他五个答案?
  • 嗨,我认为它是如此简单,无需评论,实际上你可以离开 var 并输入 TextBox [] somename = panel1.Controls.Oftype().Toarray();这您只需使用 Type ,TextBox> 收集位于 panel1 上的所有控件项,毕竟您有常量集合(意味着索引将相同,您可以管理它以进行数据写入/记录)
  • 索引将始终相同,在 array() 之后,您有恒定的索引。对于读取或写入,您不需要知道文本框名称只需创建获取信息和写入的算法,读取将使用相同的索引。
【解决方案2】:

使用LINQ,您可以通过这种方式获取具有特定 ID(键)的所有客户名称(值):

var valuesList = items.Where(x => x.Id == 1).Select(v => v.Name).ToList();

对于单个客户名称,您可以这样做:

var singleName = items.FirstOrDefault(x => x.Id == 1)?.Name;

显然,Id 可以是 1、2 或其他任何值。

编辑:

我推荐你List&lt;Customer&gt; 而不是Customer[]

所以,

var items = new List<Customer> 
{ 
     new Customer { Name = "test1", Id = 999 }, 
     new Customer { Name = "test2", Id = 989 } 
};

【讨论】:

  • 我没有得到x.Id 我得到的是x. Equals,GetHashCode,GetType,ToString
  • 使用转换为(Customer)x.Id
  • @Neo 如果您将使用List&lt;Customer&gt;,那么现在需要进行任何强制转换,否则匿名类型需要强制转换。
  • 你能看看我的question 之一吗?
【解决方案3】:

如果您想通过 Id 从集合中检索 Customer 名称:

public string GetCustomerName(IEnumerable<Customer> customers, int id)
{
    return customers.First(c => c.Id == id).Name;
}

【讨论】:

  • 我没有得到c.Id 我得到的是c. Equals,GetHashCode,GetType,ToString
  • 可能是因为您查询的是IEnumerable&lt;object&gt;,而不是IEnumerable&lt;Customer&gt;,请尝试类似customers.OfType&lt;Customer&gt;()
  • @Neo,我认为您需要包括使用 System.Linq。它给了我同样的问题,在我使用 System.Linq 后工作
  • @ArvindKrmar 你拯救了我朋友的一天
【解决方案4】:

您想在创建列表后反复查找吗?如果是这样,您可能需要考虑创建一个字典来进行查找,如下所示:

IEnumerable<Customer> items = new Customer[]
{
    new Customer {Name = "test1", Id = 999},
    new Customer {Name = "test2", Id = 989}
};

var lookup = items.ToDictionary(itemKeySelector => itemKeySelector.Id);

var result = lookup[989];

Console.WriteLine(result.Name); // Prints "test2".

我假设您首先不创建集合 - 如果您可以控制创建原始集合,则可以首先使用字典。

【讨论】:

    【解决方案5】:

    //我将propertyName作为Id传递,并且想要所有Id属性值

    //从项目集合中一一获取。

    如果我理解正确的话

    public static IEnumerable<object> GetValues<T>(IEnumerable<T> items, string propertyName)
    {
        Type type = typeof(T);
        var prop = type.GetProperty(propertyName);
        foreach (var item in items)
            yield return prop.GetValue(item, null);
    }
    

    【讨论】:

      【解决方案6】:

      只需使用 LINQ 即可实现您想做的事情。如果你想检索一个特定的值,你可以像这样使用where

      public Customer GetCustomerById(IEnumerable<Customer> items,int key)
      {
          return items.Where(x=>x.id==key)
         .Select(x =>x.Name)
         .First(); 
      }
      

      这将检索与特定 ID 匹配的客户。

      【讨论】:

      • 你能看看我的question 之一吗?
      猜你喜欢
      • 2021-11-22
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 2014-07-29
      • 2022-01-20
      相关资源
      最近更新 更多