I'm trying to do the following (SelectedIdCollection is List and cb.Id is int) -
db.Items.Where(cb => (SelectedIdCollection == null || SelectedIdCollection.Contains(cb.Id)))
Basically, if SelectedIdCollection
is null then return everything, if it is not null then filter by it.
But it throws the following error -
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code. Cannot compare elements of type 'System.Collections.Generic.IList`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'. Only primitive types, enumeration types and entity types are supported.
Is there any other way of writing this where condition?
if
(conditionalWhere
) – Ivan StoevSelectedIdCollection?.Contains(cb.Id) ?? true
work? – Patrick Hofmandb.Items.AsEnumerable().Where(cb => (SelectedIdCollection == null || SelectedIdCollection.Contains(cb.Id)))
– Nkosi