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

Entity Framework Fluid Query Start and End Dates When Start is Nullable

I have a model with following properties (Start is nullable while End is required):

public DateTime? Start { get; set; }
public DateTime End { get; set; }

I query the active events as follows:

var result = _db.ActiveEvents.Where(x => x.Start <= today && x.End >= today).ToList();

This doesn’t give me my desired result. I want the result to be within the Start and End when both Start and End have values. However, when Start is null, it must be ignored and only query for End.

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 :

Try this

var result = _db.ActiveEvents.Where(x => (x.Start == null or x.Start <= today) && x.End >= today).ToList();

Both conditions must be true for a record to be included:
The event has either started or has no start date, and
The event has not ended yet.

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