今天用Linq的Contains实现SQL下(WHERE...IN)语句,有如下错误:
“方法“Boolean Contains(System.Guid)”不支持转换为 SQL。”
源代码如下:
var result = from p in snsDC.NT_User
join q in snsDC.NT_Photo on p.Portrait equals q.PhotoID into pq
from r in pq.DefaultIfEmpty()
where userIDs.Contains(p.UserID.ToString())
select new { p.UserID, p.UserName, r.FilePath };
分析:
估计是Linq无法将IList的Contains方法正确翻译为SQL语句。
改为:
var result = from p in snsDC.NT_User
join q in snsDC.NT_Photo on p.Portrait equals q.PhotoID into pq
from r in pq.DefaultIfEmpty()
where userIDs.ToArray().Contains(p.UserID.ToString())
select new { p.UserID, p.UserName, r.FilePath };