[Solved] Extension method for List Contains(T? object) how to? [closed]


From your comments on the other answer, it looks like you’re trying to do something like

var myWantedIds = new List<int> { 1, 2, 3 };
var matchingItems = myEntityFrameworkItems
    .Where(efItem => myWantedIds.Contains(efItem.NullableIdProperty));

Writing an overload of Contains won’t help here, as it can’t be translated into SQL.

One thing to try is to check if the list contains the non-null ID:

var myWantedIds = new List<int> { 1, 2, 3 };
var matchingItems = myEntityFrameworkItems
    .Where(efItem => efItem.NullableIdProperty.HasValue &&
                     myWantedIds.Contains(efItem.NullableIdProperty.Value));

In case that also doesn’t translate, you could also try to convert your search list to the nullable version:

var myWantedIds = new List<int> { 1, 2, 3 };
var myNullableIds = myWantedIds.Select(i => new int?(i)).ToList();
var matchingItems = myEntityFrameworkItems
    .Where(efItem => myNullableIds.Contains(efItem.NullableIdProperty));

solved Extension method for List Contains(T? object) how to? [closed]