MJK
MJK

Reputation: 3514

Get a iqueryable items which do not exists on another list using lambda expression

I have an IQueryable(Of T) and a list of depended items List(of T). I am trying to achieve an IQueryable(Of T) which will exclude all the items in the List(of T).

 Dim returnQuery As IQueryable(Of POCO.ClassName) = GetTheQuery(...)
 Dim excludeLists As List(Of POCO.ClassName) = GetExcludedList(...)

 returnQuery = returnQuery.Except(excludeLists)

The error occurs when trying the execute the result for IQueryable.

Unable to create a constant value of type 'POCO.ClassName'. Only primitive types or enumeration types are supported in this context.

It seems except will not work in this case. So how can I exclude the list items using lambda expressions.

Upvotes: 1

Views: 3816

Answers (1)

Pieter21
Pieter21

Reputation: 1845

Well, the typical example: http://msdn.microsoft.com/en-us/library/vstudio/bb300779(v=vs.100).aspx uses doubles, and combined with the error you got, it is clear Except only works on primitives like doubles.

So you'll have to use Linq, and there is nothing wrong with that.

returnQuery = returnQuery.Where(item => !excludeLists.Contains(item))

Upvotes: 3

Related Questions