I have an Extension method which is supposed to filter a Queryable object (IQueryable) based upon a collection of Ids....
我有一个Extension方法,它应该根据Ids的集合过滤一个Queryable对象(IQueryable)....
Note that IQueryable is sourced from my database via a LinqToSql request
请注意,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;
}
If Ids are created from an array or list and passed in as a queryable list, it DOESNT work
如果从数组或列表创建ID并将其作为可查询列表传入,则它无法正常工作
For example...
GetNewsItemSummary().WithID(ids.AsQueryable<Guid>())
If Ids is composed form a LinqToSql request, it DOES work!!
如果Ids是由LinqToSql请求组成的,它就可以工作!!
This is known issue: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=355026
这是已知问题:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?反馈ID = 355026
My Ids collection cannot come from a LinqToSql request...
我的Ids集合不能来自LinqToSql请求......
Note, if I change the function so that it consumes and IList rather than an IQueryable....
注意,如果我更改函数使它消耗和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;
}
I now get the following exception:
我现在得到以下异常:
Method 'Boolean Contains(System.Guid)' has no supported translation to SQL.
So... all I want to do is filter my collection of news based upon a list or array of Guids.... Ideas???
所以......我想做的就是根据Guids列表或数组过滤我的新闻集合。
1 个解决方案
#1
This will translate.
这将翻译。
public static IQueryable<NewsItemSummary> WithID(
this IQueryable<NewsItemSummary> qry,
List<Guid> Ids
)
{
return from newsItemSummary in qry
where Ids.Contains(newsItemSummary.ID)
select newsItemSummary;
}
)
Translation of the Contains method against local collections was one of the last features added in the development of linq to sql for .net 3.5, so there are some cases that you would expect work that don't - such as translation of IList<T>
.
针对本地集合的Contains方法的翻译是为.net 3.5开发linq to sql时添加的最后一个特性之一,所以在某些情况下你会期望没有的工作 - 比如IList
Also, be aware that while LinqToSql will happily translate lists containing a vast number of items (I've seen it do over 50,000 elements), SQL Server will only accept 2,100 parameters for a single query.
此外,请注意,虽然LinqToSql将很乐意翻译包含大量项目的列表(我已经看到它超过50,000个元素),但SQL Server将只接受单个查询的2,100个参数。
#1
This will translate.
这将翻译。
public static IQueryable<NewsItemSummary> WithID(
this IQueryable<NewsItemSummary> qry,
List<Guid> Ids
)
{
return from newsItemSummary in qry
where Ids.Contains(newsItemSummary.ID)
select newsItemSummary;
}
)
Translation of the Contains method against local collections was one of the last features added in the development of linq to sql for .net 3.5, so there are some cases that you would expect work that don't - such as translation of IList<T>
.
针对本地集合的Contains方法的翻译是为.net 3.5开发linq to sql时添加的最后一个特性之一,所以在某些情况下你会期望没有的工作 - 比如IList
Also, be aware that while LinqToSql will happily translate lists containing a vast number of items (I've seen it do over 50,000 elements), SQL Server will only accept 2,100 parameters for a single query.
此外,请注意,虽然LinqToSql将很乐意翻译包含大量项目的列表(我已经看到它超过50,000个元素),但SQL Server将只接受单个查询的2,100个参数。