-2
I am developing an Api and in my GET method can be passed word to be searched, start date and end date. However all fields are optional, that is, I can pass only the word without date, I can pass only the dates, I can pass only initial date, I can pass the word with the dates.
What is the best way to make this condition?
I tried it this way:
if (!string.IsNullOrEmpty(statsCommand.SearchString))
where = c => c.text.Contains(statsCommand.SearchString);
if (statsCommand.StartDate != DateTime.MinValue)
where = c => c.date == statsCommand.StartDate;
if (statsCommand.StartDate != DateTime.MinValue && statsCommand.EndDate != DateTime.MinValue)
where = c => c.date >= statsCommand.EndDate && c.date <= statsCommand.StartDate;
Then I do the attribution in the result below:
result = await collection.AsQueryable()
.GroupBy(c => c.details.text)
.Select(d => new Details { text = d.First().details.text, date = d.First().details.date, Searchs = d.Count() })
.Where(where)
.OrderByDescending(c => c.Searchs)
.Skip((statsCommand.PageNumber - 1) * statsCommand.PageSize)
.Take(statsCommand.PageSize)
.ToListAsync();
I am not knowing how to make these conditions, is always coming the last true condition and not the junction of them all.
Can anyone help? Thanks in advance
You debugged your code and understand what you’re doing?
– Leandro Angelo
@Leandroangelo pq the question? Of course I understand WHAT I NEED, I do not know how to DO, so I asked here....
– Manoel Opá