【发布时间】:2010-11-01 00:09:19
【问题描述】:
我有一个扩展方法,它应该根据一组 Id 过滤一个可查询的对象 (IQueryable)....
请注意,IQueryable 是通过 LinqToSql 请求从我的数据库中获取的
public static IQueryable<NewsItemSummary> WithID(this IQueryable<NewsItemSummary> qry, IQueryable<Guid> Ids)
{
return from newsItemSummary in qry
where Ids.Contains(newsItemSummary.ID)
select newsItemSummary;
}
如果 Ids 是从数组或列表中创建并作为可查询列表传入的,则它不起作用
例如...
GetNewsItemSummary().WithID(ids.AsQueryable<Guid>())
如果 Ids 由 LinqToSql 请求组成,它确实有效!
这是已知问题: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=355026
我的 Ids 集合不能来自 LinqToSql 请求...
注意,如果我更改函数以使其使用 IList 而不是 IQueryable....
public static IQueryable<NewsItemSummary> WithID(this IQueryable<NewsItemSummary> qry, IList<Guid> Ids)
{
return from newsItemSummary in qry
where Ids.Contains(newsItemSummary.ID)
select newsItemSummary;
}
我现在得到以下异常:
Method 'Boolean Contains(System.Guid)' has no supported translation to SQL.
所以...我要做的就是根据一个列表或一组 Guid 过滤我的新闻集合...想法???
【问题讨论】:
-
你知道,“stackoverflow”标签通常用于元问题,而“stack-overflow”标签更适合真正的堆栈溢出。
标签: linq linq-to-sql contains