Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

lambda expression with nullable value – always false since the value of type Guid is never equal to 'null'

public class Car
{
    public int Id {get; set;}
    public Guid? OwnersId { get; set; }
    ...
}

I’m trying to fetch all car data upon criteria

List<Car> cars = await carsContext.Query(x=>x.Id== model.CarId && x.OwnersId.Value == null);

The result of the expression is always false since the value of type
Guid is never equal to ‘null’ of type ‘Guid?’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Value property of Nullable<T> struct returns value of the underlying type (Guid in your case) which can not be null. Compare the OwnersId itself to null:

List<Car> cars = await carsContext
    .Query(x => x.Id == model.CarId && x.OwnersId == null);

Another option is to check HasValue property:

List<Car> cars = await carsContext
    .Query(x => x.Id == model.CarId && !x.OwnersId.HasValue);

Though if this will translate into valid SQL depends on the ORM you are using.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading