Dynamic Linq to Select records with null value

147 Views Asked by At

enter image description here

Culture is dynamically selected, so that "Name_culture" param could be "name_en" or "name_it" and so on. That said, once culture is selected, all records where related Name_culture is NULL have to be filtered out.

using System.Linq.Dynamic;

IQueryable CountryTable = db.ISO3166
.AsQueryable()

This works:

.Where($"{Name_culture} = @0", "Guatemala")

This doesn't:

.Where($"{Name_culture} != @0", null)

ERROR:

System.Linq.Dynamic.ParseExceptionNo property or field '0' exists in type 'ISO3166'

Please note NOT using Dynamic Linq query (i.e. setting any fixed db field at compile time) returns expected result:

.Where(Where(i => i.name_it != null)

Returns all record BUT null, as expected.

How to compare a string to null in Dynamic Linq syntax?

1

There are 1 best solutions below

0
Luke On BEST ANSWER

This is the correct syntax:

.Where($"{Name_culture} != null")