I'm trying to implement a Search method with multiple filters on db records.
The below code works fine but when ever one of the filters is null
a:
NullReferenceException
...occurs
var user = model.Username != null ? await UserManager.FindByNameAsync(model.Username) : null;
res = DB.UserWatchActivityLogs.Where(x => x.User_Id == (user != null ? user.Id : x.User_Id) &&
x.Tutorial.Title.Contains(model.TutorialName ?? "") &&
x.DateTime >= (model.DateTimeFrom != null ? DateTime.Parse(model.DateTimeFrom, new CultureInfo("fa-IR")) : x.DateTime) &&
x.DateTime <= (model.DateTimeTo != null ? DateTime.Parse(model.DateTimeTo, new CultureInfo("fa-IR")) : x.DateTime))
.Include(x => x.User).Include(x => x.Tutorial).ToList();