Ken
Ken

Reputation: 1404

How to get a list of elements out of a list based on conditions

I want to pick elements out of a list based on if the elements in another list.

list1 = List<FileModel>(){..};
List2 = List<WindowModel>(){...};

List3 = List1.Where(d => List2.Select(x => x.FileName).Contains(d.FileName));

But I've got an error (Cannot implicitly convert System.Collections.Generic.IEnumberable<> to System.Collections.Generic.List<>). How to get it done here?

Upvotes: 0

Views: 87

Answers (2)

Aman Kedia
Aman Kedia

Reputation: 1

Use ToList<FileModel>();

List1.Where(x => List2.Select(y => y.FileName).Contains(x.FileName)).ToList<FileModel>();

Upvotes: -2

Manish
Manish

Reputation: 1182

Use ToList() Method.

List1.Where(d => List2.Select(x => x.FileName).Contains(d.FileName)).ToList();

Upvotes: 4

Related Questions