我有一个Extension方法,它应该基于一组ID来过滤一个Queryable对象(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;
}
如果 ID 是从数组或列表创建的,并作为可查询列表传入,则它无法正常工作
例如......
GetNewsItemSummary().WithID(ids.AsQueryable<Guid>())
如果 ID 是由LinqToSql请求组成的,那就可以了!
这是已知问题: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=355026
My 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.
所以...我想做的就是根据Guids列表或数组过滤我的新闻集合......想法???
答案 0 :(得分:11)
这将翻译。
public static IQueryable<NewsItemSummary> WithID(
this IQueryable<NewsItemSummary> qry,
List<Guid> Ids
)
{
return from newsItemSummary in qry
where Ids.Contains(newsItemSummary.ID)
select newsItemSummary;
}
)
针对本地集合的Contains方法的翻译是为.net 3.5开发linq to sql时添加的最后一个功能之一,因此在某些情况下,您会期望工作不会 - 例如{的翻译{1}}。
另外,请注意,虽然LinqToSql很乐意翻译包含大量项目的列表(我已经看到它超过50,000个元素),但SQL Server只接受单个查询的2,100个参数。