C# 如何使用IEnumerable集合的键从集合中获取值?

C# 如何使用IEnumerable集合的键从集合中获取值?,c#,ienumerable,ienumerator,C#,Ienumerable,Ienumerator,我有如下数不清的收藏 IEnumerable<Customer> items = new Customer[] { new Customer { Name = "test1", Id = 999 }, new Customer { Name = "test2", Id = 989 } }; 使用LINQ可以通过以下方式获得具有特定Id(键)的所有客户名称(值): var valuesList = items.Where(x => x.Id == 1

我有如下数不清的收藏

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

使用
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或任何其他

编辑:

我建议您使用
列表
而不是
客户[]

所以

var items=新列表
{ 
新客户{Name=“test1”,Id=999},
新客户{Name=“test2”,Id=989}
};

只需使用LINQ即可实现您想要做的事情。如果要检索特定值,可以使用
where
如下所示:

public Customer GetCustomerById(IEnumerable<Customer> items,int key)
{
    return items.Where(x=>x.id==key)
   .Select(x =>x.Name)
   .First(); 
}
public Customer GetCustomerById(IEnumerable items,int key)
{
返回项。其中(x=>x.id==key)
.选择(x=>x.Name)
.First();
}

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

如果要通过其
Id
从集合中检索
客户
名称:

public string GetCustomerName(IEnumerable<Customer> customers, int id)
{
    return customers.First(c => c.Id == id).Name;
}
公共字符串GetCustomerName(IEnumerable customers,int-id)
{
返回customers.First(c=>c.Id==Id).Name;
}
//我将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);
}
公共静态IEnumerable GetValues(IEnumerable项,字符串propertyName)
{
类型=类型(T);
var prop=type.GetProperty(propertyName);
foreach(项目中的var项目)
收益返回prop.GetValue(项,空);
}

创建列表后是否要重复查找?如果是这样,您可能需要考虑创建一个字典来进行查找,例如:

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".
IEnumerable items=新客户[]
{
新客户{Name=“test1”,Id=999},
新客户{Name=“test2”,Id=989}
};
var lookup=items.ToDictionary(itemKeySelector=>itemKeySelector.Id);
var结果=查找[989];
Console.WriteLine(result.Name);//打印“test2”。
我假设您首先不创建集合-如果您可以控制创建原始集合,那么您可以首先使用字典。

private TextBox[]collections textboxon Panel(Panel Panel)
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
}
{ var textBoxspanel1=panel.Controls.OfType();//按类型选择panle1上的控件 IEnumerable textboxs=textBoxspanel1;//根据需要创建集合 TextBox[]textBoxes1=textboxs.ToArray();//数组集合 return textBoxes1;//返回TextBox集合 }
为什么不直接使用Select方法?它也是类型安全的。
items.Select(x=>x.Id)
就可以了。您想实现什么?如果您想以“泛型”方式实现它,您可以为方法提供Func选择器(谓词),而不是
propertyName
参数,然后在linq where子句中使用它。与您需要选择的内容相同。如果它是整个
T
项,您不需要特定的
select
,但是如果您只需要一个特定的属性,请传递另一个选择器函数(类型为
Func
),我不获取
c.Id
我获取的是
c.
Equals,GetHashCode,GetType,ToStringprobably,因为您查询
IEnumerable
,不是
IEnumerable
,尝试类似
customers.OfType()
@Neo的方法,我认为您需要包括使用System.Linq。它给了我同样的问题,在我包括使用系统后工作。LinqI没有得到
x.Id
我得到
x.
Equals,GetHashCode,GetType,ToString转换为
(客户)x、 Id
@Neo如果您要使用
列表
则现在需要进行任何铸造,否则匿名类型需要铸造。您可以查看我的一个相关信息吗?您可以查看我的一个相关信息吗?您好Igor!用一两句话解释一下你在代码中做了些什么,这样会更容易理解。@Pluto的观点总是一个很好的实践,但在这些有既定答案的老问题上尤其重要。为什么要采用这种方法而不是其他五个答案呢?嗨,我认为它很简单,而且不需要重复,实际上你可以去掉var,只键入TextBox[]somename=panel1.Controls.Oftype().Toarray();您只需将位于panel1上的所有控件项以类型TextBox>进行集合,在您拥有常量集合之后(意味着索引将是相同的,您可以对数据写入/记录进行管理)。索引将始终相同,在array()之后,您拥有常量索引。对于读或写,您不需要知道文本框的名称,只需为take info和write创建算法,阅读将使用相同的索引。
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".
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
}