【发布时间】:2020-03-15 15:56:44
【问题描述】:
我有两个查询要按名称搜索 Authors 和按标题搜索 Books。第一个按预期工作,正在查看是否有任何作者的姓名包含我的输入。出于某种原因,我不能对书名做同样的事情。当我知道 它是 string...
char 采取行动
它们之间的唯一区别是我使用的是List<string> Names 和string Title
按“作者姓名”查询(工作)
author = from book in Serialisation.Books
where book.Author.Names.Any(author => author.Contains(InputBook.Text))
select book;
当我将鼠标悬停在author => author 上时,它告诉我这是一个字符串参数。属性 Names 是 List<string> Names,因为某些书籍可能有 2 个作者。我能够找到任何与只有一个字母的搜索相对应的作者姓名。
例如:« M » 输出 => Margaret Atwood
按“书名”查询(无效)
book = from book in Serialisation.Books
where book.Title.Any(x => x.Contains(InputBook.Text))
select book;
在这里,当我将鼠标悬停在 x => x 上时,它告诉我这是一个 char 参数,因此我不能使用方法 .Contains()...
我得到的唯一解决方案是改写这个:
book = from book in Serialisation.Books
where book.Title == InputBook.Text
select book;
这当然不是我想要的。我不知道要改变什么才能让它工作..
编辑:
我试过book.Title.Contains(InputBook.Text),后来我收到一个错误,告诉我在转换输出时我不能得到空值。ToList()
课本
public class Book
{
public string Title { get; set; }
public Author Author { get; set; }
// my other class Author is simply a list of names.
// I need it to override the method ToString() so that
// when there is two authors for the same book, I only have
// one string to look into for my query.
}
【问题讨论】:
-
为什么不使用
book.Title.Contains(InputBook.Text)?我是否误解了您的要求? -
不,你没有。我已经尝试过了,但是无论我在输入中尝试什么,我都会收到 NullException。
-
我认为您需要为此忽略文化案例。看看@tymtam 的答案