简体   繁体   English

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

[英]C# list object value is in other type list/array

I was wondering if there was a function in the list class that would allow me to select all the objects that have a 我想知道列表类中是否有一个函数可以让我选择所有具有

for example... 例如...

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

I want to see where the list's names and the names array intersect. 我想看看列表的名称和名称数组在哪里相交。 the only way I could think of involves inner and outer loops, I'm trying to think of an alternative to that. 我能想到的唯一方法涉及内部和外部循环,我正在尝试一种替代方法。

Thanks in advance 提前致谢

在以下情况下, ContainsMSDN )是您的朋友:

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

You could use join 您可以使用join

var allPeopleWithNamesinNamesArray = from p in randomListOfPeople
                                     join n in names on p.fname equals n
                                     select p;

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

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