对于方法 String.Contains,只支持可在客户端上求值的参数。 --这是怎么回事?
代码如下:
var select = from ar in adc.Articles
from au in adc.Authors
where ar.ShowTime > timeset && ar.AuthorType == typeid && ar.Author.Contains(au.Name) == true && ar.Title.Contains(au.Name) == true
orderby ar.ShowTime descending
select new
{
arid = ar.Id,
ar.Code,
ar.Title,
ar.ShowTime,
auid = au.Id,
au.Name,
au.PinYin,
au.GoUrl
};
foreach (var item in select)
{
AuthorArticle aa = new AuthorArticle();
aa.ArticleId = item.arid;
aa.ArticleCode = item.Code;
aa.ArticleTitle = item.Title;
aa.ArticleShowTime = item.ShowTime;
aa.AuthorId = item.auid;
aa.AuthorName = item.Name;
aa.AuthorNamePY = item.PinYin;
aa.AuthorGoUrl = item.GoUrl;
aalist.Add(aa);
}
[最优解释]
Linq to SQL限制了一些方法。你可以考虑使用join等它支持可以直接转换为T-SQL语句的方法。
如果一定要使用它不支持的计算方法,你可以先使用Linq to SQL查询出可以查询的那些结果,然后在结果的 ToList() 的基础上再用一条 Linq to object查询来继续你的计算。
不过应该首先改为它支持的查询方法为好。
[其他解释]
我把 ar.Author.Contains(au.Name) == true 写成 ar.Author.IndexOf(au.Name) > -1 ,就可以了。
不知这样写会不会影响效率?因为 ar.Author.IndexOf(au.Name) > -1 最后生成的SQL语句变成这样
((
(CASE
WHEN (CONVERT(Int,DATALENGTH([t1].[Aut_Name]) / 2)) = 0 THEN CONVERT(BigInt,0)
ELSE CONVERT(BigInt,(CONVERT(Int,CHARINDEX([t1].[Aut_Name], [t0].[Art_Author_Name]))) - 1)
END)) > -1)
而 ar.Author.Contains(au.Name) == true 是生成的SQL语句是
like '%查询字符%'
[其他解释]
如果是单表查询,将 var select 改成 IEnumerable<对象模型> ,Contains 就可以使用了。
可惜我这是多表查询
[其他解释]
是奇怪!我遇到了!
[其他解释]
该回复于2012-02-28 15:03:53被版主删除
[其他解释]
最后这个地方:ar.Title.Contains(au.Name) == true 转换成数组string[]类型,即可。