Using Autoquery to get data from an MVC Application

Jay Vijay Modi 61 Reputation points
2021-07-11T07:20:24.277+00:00

So I have created a basic MVC Application with the CRUD operations using Entity Framework. But I have been told to add AutoQuery in the application so as to when the user says /customer?select=customerId,name,color,customerCategory.name,salesOrderDetail,salesOrderDetail.customer then it should return a JSON format of the data that are selected. I am confused as to from where to start if I'm using autoquery.
I have 5 entities Customer, Team, Session, SessionEventLog and Request Category. I have created a CustomerController for performing customer data CRUD operations but this is not Autoquery as what I'm doing here is this:

public ActionResult Details(int id)
        {
            using (DatabaseContext DB = new DatabaseContext ())
            {
                return View(DB.Customers.Where(x=> x.RefNo == id).FirstOrDefault());
            }                    
        }

This is retrieving all the values but what I want is when I put /customer?select=customerId,name,color,customerCategory.name,salesOrderDetail,salesOrderDetail.customer in the url it will only retrieve these particular values.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,346 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,246 questions
0 comments No comments
{count} votes

Accepted answer
  1. Duane Arnold 3,211 Reputation points
    2021-07-11T10:39:16.267+00:00

    What you want is a Linq projection using a custom class. The custom class is sent into the view.

    https://csharp-station.com/Tutorial/Linq/Lesson02

    But a couple of other things here like understanding the MVC models like a viewmodel as opposed to the persistence model.

    The custom class for the Linq projection would be the viewmodel.

    https://deviq.com/terms/kinds-of-models

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    You map persistence model object over to a viewmodel object and the viewmodel object is mapped to a persistence model object for data persistence with the database.

    understand seperation of concerns.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    As talked about with the subject on Understanding Models, keep the controller thin void of any database logic that should be in a class in the models folder with methods called upon by the controller.

    Example of a MVC solution you can review implementing the things discussed. VM = viewmodel and DM = domain model. The example is doing CRUD with the database using EF sitting behind the WebAPI in the DAL.

    Just know that the database logic belongs in a class in the Models folder not in the controller keep the controller thin.

    https://github.com/darnold924/PublishingCompany

    HTH

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,041 Reputation points
    2021-07-11T16:43:51.507+00:00

    You can use a dynamic expression tree you build for the select. See this thread

    https://stackoverflow.com/questions/16516971/linq-dynamic-select

    0 comments No comments