MicrosoftDocs / OData-docs

Creative Commons Attribution 4.0 International
33 stars 110 forks source link

Update getting-started.md #296

Open michelcedric opened 1 year ago

michelcedric commented 1 year ago

Adapt get method by key

An odata endpoint must return a IQueryable. If you evaluate it directly in the return of the controller method, the odata query feature (example : expand) can't working. If you directly return the queryable you return an array with only one element. You can fix that with the usage of SingleResult.Create method form odata

learn-build-service-prod[bot] commented 1 year ago

Learn Build status updates of commit d8c75bd:

:white_check_mark: Validation status: passed

File Status Preview URL Details
Odata-docs/webapi-8/getting-started.md :white_check_mark:Succeeded View

For more details, please refer to the build report.

For any questions, please:

learn-build-service-prod[bot] commented 1 year ago

Learn Build status updates of commit 4933754:

:white_check_mark: Validation status: passed

File Status Preview URL Details
Odata-docs/webapi-8/getting-started.md :white_check_mark:Succeeded View

For more details, please refer to the build report.

For any questions, please:

learn-build-service-prod[bot] commented 1 year ago

Learn Build status updates of commit 3602f07:

:white_check_mark: Validation status: passed

File Status Preview URL Details
Odata-docs/webapi-8/getting-started.md :white_check_mark:Succeeded View
Odata-docs/webapi-8/tutorials/basic-crud.md :white_check_mark:Succeeded View

For more details, please refer to the build report.

For any questions, please:

michelcedric commented 1 year ago

@microsoft-github-policy-service agree

learn-build-service-prod[bot] commented 1 year ago

Learn Build status updates of commit e9d9d88:

:white_check_mark: Validation status: passed

File Status Preview URL Details
Odata-docs/webapi-8/getting-started.md :white_check_mark:Succeeded View
Odata-docs/webapi-8/tutorials/basic-crud.md :white_check_mark:Succeeded View

For more details, please refer to the build report.

For any questions, please:

gathogojr commented 11 months ago

@michelcedric There's no reason for the changes that you proposed. Query options work okay even when the return type is ActionResult or ActionResult<T>. If you have reasons to believe that they don't, please report the issue together with a simple repro on https://github.com/OData/AspNetCoreOData/issues.

michelcedric commented 10 months ago

@gathogojr I'm sorry, I will give more details. But If you use a real Database as Sql Server with EF. When you execute. var customer = db.Customers.SingleOrDefault(d => d.Id == key); The query is immediately evaluated and executed on the database. Customer object is loaded in memory and in this case expand is not possible. It's that why the return of the controller must be a queryable to delay to Odata the execution of the query. For an id the return is not a queryable but only the entity. It's seems the reasons why SingleResult of Odata exists. var item = customers.AsQueryable().Where(c => c.Id == id); return SingleResult.Create(item); I created a repository to exactly show what I mean https://github.com/michelcedric/OdataInclude/blob/main/OdataInclude/Controllers/BooksController.cs https://github.com/michelcedric/OdataInclude/blob/main/OdataInclude/OdataInclude.http image 1 Get books to retrieve the first ID 2 Get book with the ID and expand MainAuthor=> MainAuthor null 3 Get book with the ID and expand MainAuthor version 2 => MainAuthor filled 4 Get book with bad ID and expand MainAuthor=> Not found 404.

gathogojr commented 2 months ago

@gathogojr I'm sorry, I will give more details. But If you use a real Database as Sql Server with EF. When you execute. var customer = db.Customers.SingleOrDefault(d => d.Id == key); The query is immediately evaluated and executed on the database. Customer object is loaded in memory and in this case expand is not possible. It's that why the return of the controller must be a queryable to delay to Odata the execution of the query. For an id the return is not a queryable but only the entity. It's seems the reasons why SingleResult of Odata exists. var item = customers.AsQueryable().Where(c => c.Id == id); return SingleResult.Create(item); I created a repository to exactly show what I mean https://github.com/michelcedric/OdataInclude/blob/main/OdataInclude/Controllers/BooksController.cs https://github.com/michelcedric/OdataInclude/blob/main/OdataInclude/OdataInclude.http image 1 Get books to retrieve the first ID 2 Get book with the ID and expand MainAuthor=> MainAuthor null 3 Get book with the ID and expand MainAuthor version 2 => MainAuthor filled 4 Get book with bad ID and expand MainAuthor=> Not found 404.

@michelcedric The sample code in this tutorial uses an in-memory collection as a data store. There's therefore no value in returning a SingleResult in this case. There's also no value in casting the collection to AsQueryable.

In my opinion, it'd be more ideal to have a different article with code samples that are relevant to an SQL Server data store where we can employ the use of SingleResult.Create to construct and execute the query for the expanded entity at the database level...