我有两个查询,分别按名称搜索Authors
和按标题搜索Books
。第一个按预期方式工作,它正在查看是否有任何作者的姓名包含我的输入。由于某些原因,我无法对这些书的书名进行相同的处理。我收到一条错误消息,说我知道 是char
...
string
采取行动
它们之间的唯一区别是我使用的是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
上时,它告诉我这是一个字符串参数。名称属性是List<string> Names
,因为有些书可能有2位作者。我能够找到只用一个字母进行搜索的作者姓名。
例如:«M»输出=>玛格丽特·阿特伍德
按“书名”查询(不起作用)
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.
}