Joe Jazdzewski
Joe Jazdzewski

Reputation: 733

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

Upvotes: 1

Views: 80

Answers (2)

Saffar
Saffar

Reputation: 149

You could use join

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

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61369

Contains (MSDN) is your friend in these situations:

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

Upvotes: 4

Related Questions