Happy Coconut
Happy Coconut

Reputation: 1063

Find an Object that contains a string in a list of strings using LINQ

I have these lists:

List<Author> MyAuthorList = new List<Author>();
List<string> BookListNo1 = new List<string>() { "The Girl with the Dragon Tattoo", "The Name of the Rose", "The Alienist", "In Cold Blood", "The Firm" };
List<string> BookListNo2 = new List<string>() { "And Then There Were None", "Mystic River", "The Shadow of the Wind", "Angels & Demons" , "The Big Sleep", "The Pelican Brief" };
List<string> BookListNo3 = new List<string>() { "One for the Money", "The Maltese Falcon", "In the Woods", "Presumed Innocent", "The Thirteenth Tale", "A is for Alibi", "Postmortem" };
List<string> BookListNo4 = new List<string>() { "Midnight in the Garden of Good and Evil", "The Strange Case of Dr. Jekyll and Mr. Hyde", "A Time to Kill", "The Historian" };

MyAuthorList.Add(new Author() { FirstName = "John", LastName = "Smith", Address = "Germany", Age = 13, NumberOfBooks = 5, EMBG = 123123, Books = BookListNo1, BankAccount = 1111, BankName = "Stupid Bank Name", BankAddress = "No One Knows" });
MyAuthorList.Add(new Author() { FirstName = "Max", LastName = "Warren", Address = "France", Age = 32, NumberOfBooks = 6, EMBG = 321321, Books = BookListNo2, BankAccount = 2222, BankName = "Stupid Bank Name", BankAddress = "Near The Bakery" });
MyAuthorList.Add(new Author() { FirstName = "Quinn", LastName = "Swanson", Address = "Russia", Age = 11, NumberOfBooks = 7, EMBG = 456456, Books = BookListNo3, BankAccount = 3333, BankName = "Stupid Bank Name", BankAddress = "On Some Desert Island" });
MyAuthorList.Add(new Author() { FirstName = "Ben", LastName = "Chaplin", Address = "Indonesia", Age = 34, NumberOfBooks = 4, EMBG = 654654, Books = BookListNo4, BankAccount = 4444, BankName = "Stupid Bank Name", BankAddress = "Moskovska 45" });
MyAuthorList.Add(new Author() { FirstName = "Jack", LastName = "Smirnoff", Address = "Germany", Age = 35, NumberOfBooks = 6, EMBG = 789789, Books = BookListNo2, BankAccount = 5555, BankName = "Stupid Bank Name 2", BankAddress = "Moskovska 452" });

Now I need to find all Authors from Germany with books that have the words "Girl" and "Blood" in them.

This is what I have tried so far:

I get all the authors from Germany like this:

var germanAuthors = MyAuthorList.Where(x => x.Address.Contains("Germany"));

..and all the books that have the words "Blood" and "Girl" in them like this:

var BooksOfAuthorsFromGermany = MyAuthorList.Where(x => x.Address.Contains("Germany")).SelectMany(y => y.Books);
List<string> words = new List<string> { "Blood", "Girl"};
var searchedListOfBooks = BooksOfAuthorsFromGermany.Where(s => words.Any(w => s.Contains(w)));

However, I can't combine these two together.

Do I need to do this in a completely different way?

Upvotes: 2

Views: 3164

Answers (3)

Manish Kumar
Manish Kumar

Reputation: 507

Try this

var BooksOfAuthorsFromGermany = MyAuthorList
                              .Where(x => x.Address.Contains("Germany") 
                                       && x.Books.Where(a => a.Contains("Girl") 
                                                         || a.Contains("Blood")).Count() > 0)
                              .ToList();

Upvotes: 3

Abhilash Ravindran C K
Abhilash Ravindran C K

Reputation: 1856

You can combine two query as follows ,

List<string> words = new List<string> { "Blood", "Girl"};
var AuthorList = MyAuthorList.Where(x => x.Address.Contains("Germany") && x.Books.Any(a => words.Any(w=> a.Contains(w)))).ToList();

Upvotes: 1

Gagan Burde
Gagan Burde

Reputation: 328

According to your requirement you can use this

var germanAuthors = MyAuthorList.Where(x => x.Address.Contains("Germany") && x.Books.Any(y => y.Contains("Blood") && y.Contains("Girl"))).Select(Z => Z.FirstName);
var germanAuthorsWithBloodOrGirl = MyAuthorList.Where(x => x.Address.Contains("Germany") && x.Books.Any(y => y.Contains("Blood") || y.Contains("Girl"))).Select(Z => Z.FirstName);

Upvotes: 0

Related Questions