Reputation: 11403
Q:
I have a list of objects like this:
List<object> entities = GetallEntities();
The first object in the previous list is : List<Books>
.
Now i wanna to get all the books which id
exist in the following list:
List<string> BookIds = new List<string>();
Upvotes: 0
Views: 84
Reputation: 2972
public class Book
{
public string id { get; set; }
}
...
List<object> entities = new List<object> { new Book { id = "1" }, new Book { id = "2" } };
List<string> bookIds = new List<string> { "2" };
IEnumerable<object> books = entities.Where(e => e is Book && bookIds.Contains(((Book)e).id));
Upvotes: 2
Reputation: 16042
Assuming that your Books
class has an Id
property and one instance represents one book:
// Get the first element of your entity-list which is of type `List<Books>`
List<Books> bookList = entities.OfType<List<Books>>().First();
List<string> BookIds = new List<string>();
// fill your id list here ...
// filter your books by the list of ids
IEnumerable<Books> filteredBooks =
bookList.Where(b => BookIds.Any(id => id == b.Id);
Upvotes: 2