Asscending:
// GET: api/Coffee
[HttpGet]
public IActionResult GetAll()
{
try
{
var coffees = _coffeeManager.GetAll();
return Ok(coffees);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
Above this method i tried to get list of Data array where it return data by default Ascending Order. We also can create functionality for return ascending order data same as below code:
// GET: api/Coffee
[HttpGet]
public IActionResult GetAll()
{
try
{
var coffees = _coffeeManager.GetAll().OrderBy(u=>u.CreatedDate);
return Ok(coffees);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
Here, i created ascending order by CreatedDate. We also can use others properties for doing ascending order.
Output:
[
{
\"id\": 1,
\"title\": \"new string\",
\"description\": \"new description string\",
\"createdDate\": \"2023-06-21T01:56:41.971\"
},
{
\"id\": 2,
\"title\": \"string 2\",
\"description\": \"string 2\",
\"createdDate\": \"2023-06-24T07:24:40.814567\"
},
{
\"id\": 3,
\"title\": \"string 3\",
\"description\": \"string 3\",
\"createdDate\": \"2023-06-24T07:24:48.008345\"
},
{
\"id\": 4,
\"title\": \"string 4\",
\"description\": \"string 4\",
\"createdDate\": \"2023-06-24T07:24:53.902131\"
},
{
\"id\": 5,
\"title\": \"string 5\",
\"description\": \"string 5\",
\"createdDate\": \"2023-06-24T07:25:05.55175\"
},
{
\"id\": 6,
\"title\": \"string 6\",
\"description\": \"string 6\",
\"createdDate\": \"2023-06-24T07:25:27.125118\"
}
]
Decending:
// GET: api/Coffee
[HttpGet]
public IActionResult GetAllOrderByDecending()
{
try
{
var coffees = _coffeeManager.GetAll().OrderByDescending(u=>u.CreatedDate);
return Ok(coffees);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
It's same as ascending order. Only changed method name OrderByDescending. It will return us decending order data.
Output:
[
{
\"id\": 6,
\"title\": \"string 6\",
\"description\": \"string 6\",
\"createdDate\": \"2023-06-24T07:25:27.125118\"
},
{
\"id\": 5,
\"title\": \"string 5\",
\"description\": \"string 5\",
\"createdDate\": \"2023-06-24T07:25:05.55175\"
},
{
\"id\": 4,
\"title\": \"string 4\",
\"description\": \"string 4\",
\"createdDate\": \"2023-06-24T07:24:53.902131\"
},
{
\"id\": 3,
\"title\": \"string 3\",
\"description\": \"string 3\",
\"createdDate\": \"2023-06-24T07:24:48.008345\"
},
{
\"id\": 2,
\"title\": \"string 2\",
\"description\": \"string 2\",
\"createdDate\": \"2023-06-24T07:24:40.814567\"
},
{
\"id\": 1,
\"title\": \"new string\",
\"description\": \"new description string\",
\"createdDate\": \"2023-06-21T01:56:41.971\"
}
]
Multiple Condition:
// GET: api/Coffee
[HttpGet]
public IActionResult GetAllOrderByDesc()
{
try
{
var coffees = _coffeeManager.GetAll().OrderByDescending(u => u.CreatedDate).ThenByDescending(u=>u.Title);
return Ok(coffees);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
0 Comments
Leave a Comment