user1745482
user1745482

Reputation: 81

Searching Error in Linq when checking for nulls

I have a code like this:

var organisations = Uow.Query<Organisation>()
                       .Where(x => x.PersonOrganisationRoles.Any(por =>
                           por.Person.FirstName != null &&
                           por.Person.FirstName.ToLower().Contains(searchFragmentLower)));

So there is a bridging table between Organisation Person and Role. This query is trying to find orgs that have people with a FirstName containing the string.

The problem is when I run it I get:

Cannot use Person[FirstName <> NULL] as part of a logical expression in an Any or All expression

How can I check for null?

Upvotes: 1

Views: 56

Answers (2)

EdmundYeung99
EdmundYeung99

Reputation: 2511

Looks like something is complaining about the SQL that Linq is generating

try moving the null check into a where clause before running the any

var organisations = Uow.Query<Organisation>().
Where(x => x.PersonOrganisationRoles.Where(por => por != null && por.Person != null && por.Person.FirstName != null).Any(por => phr.Person.FirstName.ToLower().Contains(searchFragmentLower)));

Upvotes: 0

Mayank
Mayank

Reputation: 8852

did you try por => !string.IsNullOrEmpty(por.Person.FirstName)?

Upvotes: 2

Related Questions