Zoinky
Zoinky

Reputation: 5029

EF Core - add conditional where based on field value

I would like to add additional where clauses to my query based on the value of a of a field (dateType)

I tried the ternary operator but that doesnt work, here is the start of my query.

        var query = from e in context.Events
            join c in context.EventCategories on e.CategoryId equals c.CategoryId
            join o in context.Owners on e.OwnerId equals o.OwnerId
            where !e.IsDeleted 
                  && (e.DateType == 1 ? (e.StartDateTimeUtc <= DateTime.UtcNow && e.EndDateTimeUtc =>DateTime.UtcNow))

using EF Core 2.2

Upvotes: 0

Views: 629

Answers (1)

Zoinky
Zoinky

Reputation: 5029

Final answer thanks to Gert Arnold providing direction in comments to OP

    var query = from e in context.Events
                    join c in context.EventCategories on e.CategoryId equals c.CategoryId
                    join o in context.Owners on e.OwnerId equals o.OwnerId
                    where !e.IsDeleted && (e.DateType == 1 && e.StartDateTimeUtc <= DateTime.UtcNow && e.EndDateTimeUtc >= DateTime.UtcNow) //specific date
                                           || (e.DateType == 2 && e.Period >= DateTime.UtcNow.Month && e.Year >= DateTime.UtcNow.Year) // month
                                           || (e.DateType == 3 && e.Period >= DateTime.UtcNow.Month / 3 && e.Year >= DateTime.UtcNow.Year) //quarter

Upvotes: 1

Related Questions