From d8c75bd5adc1660ab2414e69489434e83cf12791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Mon, 22 May 2023 12:49:26 +0200 Subject: [PATCH 1/4] Update getting-started.md Adapt get method by key --- Odata-docs/webapi-8/getting-started.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Odata-docs/webapi-8/getting-started.md b/Odata-docs/webapi-8/getting-started.md index 2fa35e5f..cbdd554d 100644 --- a/Odata-docs/webapi-8/getting-started.md +++ b/Odata-docs/webapi-8/getting-started.md @@ -150,14 +150,8 @@ namespace Lab01.Controllers [EnableQuery] public ActionResult Get([FromRoute] int key) { - var item = customers.SingleOrDefault(d => d.Id.Equals(key)); - - if (item == null) - { - return NotFound(); - } - - return Ok(item); + var item = customers.AsQueryable().Where(c=>c.Id==key); + return SingleResult.Create(item); } } } From 49337544d6491e5236289274418dd6c7daa9cea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Mon, 22 May 2023 13:22:12 +0200 Subject: [PATCH 2/4] adapt return of get method --- Odata-docs/webapi-8/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Odata-docs/webapi-8/getting-started.md b/Odata-docs/webapi-8/getting-started.md index cbdd554d..9edf9de1 100644 --- a/Odata-docs/webapi-8/getting-started.md +++ b/Odata-docs/webapi-8/getting-started.md @@ -148,7 +148,7 @@ namespace Lab01.Controllers } [EnableQuery] - public ActionResult Get([FromRoute] int key) + public SingleResult Get([FromRoute] int key) { var item = customers.AsQueryable().Where(c=>c.Id==key); return SingleResult.Create(item); From 3602f072220afef274baca92b226524fd1101c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Mon, 22 May 2023 13:28:14 +0200 Subject: [PATCH 3/4] adapt get method --- Odata-docs/webapi-8/tutorials/basic-crud.md | 24 ++++++--------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/Odata-docs/webapi-8/tutorials/basic-crud.md b/Odata-docs/webapi-8/tutorials/basic-crud.md index 5982297e..5b3145f5 100644 --- a/Odata-docs/webapi-8/tutorials/basic-crud.md +++ b/Odata-docs/webapi-8/tutorials/basic-crud.md @@ -376,20 +376,14 @@ The following JSON payload shows the expected response: ## Request a single entity To support this request, we add a controller action named `Get` (or `GetCustomer`) to the `CustomersController` class. The action should accept a single parameter named `key` of type `int` - same type as the entity's key property: ```csharp -public ActionResult Get([FromRoute] int key) +public SingleResult Get([FromRoute] int key) { - var customer = db.Customers.SingleOrDefault(d => d.Id == key); - - if (customer == null) - { - return NotFound(); - } - - return Ok(customer); + var item = customers.AsQueryable().Where(c => c.Id == key); + return SingleResult.Create(item); } ``` -In the above block of code, we're using `LINQ` to write an expression to retrieve a entity with the specified key from the `Customers` table. We return a status 404 `NotFound` if we don't find any match; otherwise we return the matched entity. +The SingleResult.Create method return a status 404 `NotFound` if we don't find any match; otherwise we return the matched entity. The `FromRoute` attribute is only added for documentation purposes to show that the parameter should be bound using route-data from the current request - it is neither necessary nor mandatory. @@ -398,14 +392,8 @@ Note that for the built-in routing conventions to route the request successfully [HttpGet("odata/Customers({id})")] public ActionResult Get([FromRoute] int id) { - var customer = db.Customers.SingleOrDefault(d => d.Id == id); - - if (customer == null) - { - return NotFound(); - } - - return Ok(customer); + var item = customers.AsQueryable().Where(c => c.Id == id); + return SingleResult.Create(item); } ``` From e9d9d8875135b004ea691a49b30476659bc745e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Mon, 22 May 2023 13:28:54 +0200 Subject: [PATCH 4/4] improve space in the code --- Odata-docs/webapi-8/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Odata-docs/webapi-8/getting-started.md b/Odata-docs/webapi-8/getting-started.md index 9edf9de1..cf6f86d9 100644 --- a/Odata-docs/webapi-8/getting-started.md +++ b/Odata-docs/webapi-8/getting-started.md @@ -150,7 +150,7 @@ namespace Lab01.Controllers [EnableQuery] public SingleResult Get([FromRoute] int key) { - var item = customers.AsQueryable().Where(c=>c.Id==key); + var item = customers.AsQueryable().Where(c => c.Id == key); return SingleResult.Create(item); } }