.NET: Conditional Sort Direction in Entity Framework

10/17/2019 5:35:04 PM
The following function uses Entity Framework to sort the results. The direction of the sort is based on the input parameter.

public async Task> List__Conditional_Sort(string sort_direction)
{
	var query = from users in this._DbContext.Users

				select users;

	if (sort_direction == "asc")
	{
		query = query.OrderBy(x => x.Id);
	}
	else
	{
		query = query.OrderByDescending(x => x.Id);
	}


	return await query.ToListAsync();
}