C# 使用LINQ在字符串列表中查找包含字符串的对象

C# 使用LINQ在字符串列表中查找包含字符串的对象,c#,linq,C#,Linq,我有以下清单: 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&g

我有以下清单:

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" });
…所有的书中都有“血”和“女孩”这样的词:

var germanAuthors = MyAuthorList.Where(x => x.Address.Contains("Germany"));
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)));
然而,我不能把这两者结合起来

我需要用完全不同的方式来做吗?

试试这个

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

根据你的要求,你可以使用这个

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);

您可以按如下方式组合两个查询:

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();

1.A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【句意】A)【。是的,any较短。希望我没有在这里碰碰运气,但我会如何使用any?HappyCoconut你会将其替换。Where…Count>0替换为。any…另一个注意:如果你真的想匹配你在问题中所说的包含血和女孩的书名,你需要使用&&而不是| |。虽然我认为你的意思是说血或女孩,但以防万一。