Consider this function :
public List<Employees> getEmployees(....... , int? takeMax = null)
{
// some code
...
var items = DB_Context.Employees.Where(%%%%% WHATEVER %%%%).Take(takeMax.HasValue && takeMax.Value > 0 ? takeMax.Value : ?????? ).ToList();
}
How can I take all the items in case takeMax
is NULL?
The Take()
takes int
, and I don't want to write something like
int total = DB_Context.Employees.Count();
var items = DB_Context.Employees.Where(%%%%% WHATEVER %%%%).Take(takeMax.HasValue && takeMax.Value > 0 ? takeMax.Value : total ).ToList();
Any alternative ?
You could only apply the take in that case:
var itemsQuery = DB_Context.Employees.Where(%%%%% WHATEVER %%%%);
if (takeMax > 0)
{
itemsQuery = itemsQuery.Take(takeMax);
}
var items = itemsQuery.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With