Skip to main content
 首页 » 编程设计

C# 列表对象值在其他类型的列表/数组中

2024年02月06日12落叶

我想知道列表类中是否有一个函数可以让我选择所有具有

的对象

例如...

string [] names = {"matthew", "mark", "luke", "john"}; 
List<personObject> randomListOfPeople = generateRandomListOfPeople(); 
// suedo code start 
List<personObject> allPeopleWithNamesinNamesArray = randomListOfPeople.Where( x => x.fname isin names) 
// suedo code end 
 
public class personObject 
{ 
    public string fname {get; set;} 
    public double height {get; set;} 
    ..... 
} 

我想查看列表的名称和名称数组相交的位置。 我能想到的唯一方法涉及内部和外部循环,我正在尝试考虑替代方法。

提前致谢

请您参考如下方法:

包含 ( MSDN ) 在这些情况下是你的 friend :

List<personObject> finalList = peopleList.Where( x => names.Contains(x.fname));