亲爱的LINQ或linq2sql专家,你能帮我解决这个案子吗?
我有Books,每本书都有一个列表,我想要的是查找作者列表中的作者姓氏包含特定searchAuthor字符串的图书,如下所示:
string searchAuthor = "foo";
IQUeryable books = BookStaticRepository.SelectAll();
books.Where(r => r.Authors.Any(x => searchAuthor.Contains(x.lastname, StringComparison.OrdinalIgnoreCase)));
我得到的是这个错误:
方法“布尔包含( System.String,System.String,System.StringComparison)”不支持转换为SQL.
--我已经尝试了同样的方法,但是使用了IEnumerable,并且使用了。由于性能问题,我不得不转到IQueryable ..。我需要它和IQueryable一起工作。
谢谢你的帮助。
发布于 2011-11-08 08:20:26
嗨,伙计们,谢谢我自己找到了解决办法:
books.Where(r => r.Authors.Any(x => x.lastname.Contains(searchAuthor)));
-I认为的错误-是我在做一种“相反的工作”:我说的是“姓氏必须包含searchAuthor字符串”,而不是反之亦然(姓氏必须包含SearchAuthor字符串.这给了我我期待的结果)。
有趣的是,我在最初的问题中发布的同样的查询在使用IEnumerable时运行良好。
发布于 2011-11-07 08:23:53
这个特定的Contains
重载不起作用,但是忽略StringComparison
将使其转换为SQL,这将生成一个LIKE %SearchInput%
。默认情况下,这将是不敏感的,除非您的DB被设置为大小写感知。
就像这样:
string searchAuthor = "foo";
IQUeryable books = BookStaticRepository.SelectAll();
books.Where(r => r.Authors.Any(x => searchAuthor.Contains(x.lastname)));
https://stackoverflow.com/questions/8039016
复制相似问题