首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LINQ查找字符串列表中包含字符串的对象

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

Stack Overflow用户
提问于 2018-03-29 04:04:55
回答 3查看 2K关注 0票数 2

我有以下清单:

代码语言:javascript
复制
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" });

现在我需要找到所有来自德国的作家,他们的书里都有“女孩”和“血”的字样。

,这是我迄今为止尝试过的:

所有的作者都是这样来自德国的:

代码语言:javascript
复制
var germanAuthors = MyAuthorList.Where(x => x.Address.Contains("Germany"));

..and所有在书中写着“血”和“女孩”的书都是这样的:

代码语言:javascript
复制
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)));

但是,我不能把这两者结合在一起。

我需要用完全不同的方式做这件事吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-29 04:24:31

尝尝这个

代码语言:javascript
复制
var BooksOfAuthorsFromGermany = MyAuthorList
                              .Where(x => x.Address.Contains("Germany") 
                                       && x.Books.Where(a => a.Contains("Girl") 
                                                         || a.Contains("Blood")).Count() > 0)
                              .ToList();
票数 3
EN

Stack Overflow用户

发布于 2018-03-29 05:17:53

可以按以下方式组合两个查询,

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2018-03-29 04:34:24

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

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49547851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档