C #, compare two different types of lists

I have a list of objects and I have a list of strings.

List<String> states = new List<String>();
states.add("wisconsin");
states.add("Florida");
states.add("new york");

List<Foo> foo = new List<Foo>();
foo.add(new Foo(2000, name1, "wisconsin"));
foo.add(new Foo(1000, name2, "california"));
foo.add(new Foo(300, name3, "Florida"));

An object has three properties: int age, row name, and row state.

and I added these objects to the list. The second list consists of a line of "states".

How can I compare these two lists? What is the best way to do this? I want to know if one of the objects has the same “state” that the other list consists of. Please guide me.

+1
source share
1 answer

It looks like you want something like:

List<Person> people = ...;
List<string> states = ...;

var peopleWithKnownStates = people.Where(p => states.Contains(p.State));

Or just find if any of the people have known conditions:

var anyPersonHasKnownState = people.Any(p => states.Contains(p.State));

LINQ - , . .

, states HashSet<string>, Contains .

+4

Source: https://habr.com/ru/post/1671576/


All Articles