Sorting db info for MVC model

asp.net-mvc c# dapper mysql

Question

FindByCounty() This function should go out to the db and pull the information. Store the information in their respective classes (which it does)...but then what?

public EditCountyViewModel FindByCounty(string countyName)
        {
            var parameters = new DynamicParameters();
            parameters.Add("@CountyName", value: countyName);

            var query = @"SELECT counties.id
                            , counties.CountyName
                            , counties.Website
                            , counties.Address
                            , counties.City
                            , counties.State
                            , counties.PhonePrimary
                            , counties.PhoneAlt
                            , counties.RecordsOnline
                            , counties.BackToYear
                            , counties.Cost
                            , products.ProductName
                            , products.Description
                            , countyproduct.TurnTime_MinHours
                            , countyproduct.TurnTime_MaxHours
                            , countyproduct.Price
                        FROM
                            counties, countyproduct, products
                        WHERE
                            counties.CountyName = @CountyName AND countyproduct.countiesID = counties.ID AND countyproduct.productsID = products.ID;";

            EditCountyViewModel editCountyVM = new EditCountyViewModel();
        EditProductsViewModel editProductVM = new EditProductsViewModel();
        editProductVM.ProductList = this.db.Query<Product>(query, new { countyName }).ToList();
        editCountyVM.county = this.db.Query<County>(query, new { countyName }).FirstOrDefault();


        return editCountyVM;
        }

Details() on HOMEcontroller I am thinking i should just query all the information and then use LINQ in this function to piece out the information i need.

public ActionResult Details(string countyName)
    {
        var model = repository.FindByCounty(countyName);
        return View(model);
    }

As my code stands, It does not run because in the FindByCounty() it is returning only one of the needed elements editCountyVM. Now i could change the output of the function and return the other editProductVM but then i wouldn't have the first set of info (editCountyVM).

I believe the problem is the method i am going about obtaining this information. Any help would be appreciated.

Accepted Answer

It sounds like if you want to return both editCountyVM and editProductVM you could change the return type of your method to a Tuple<EditCountyViewModel,EditProductsViewModel>. Then make your return statement as follows:

return new Tuple<EditCountyViewModel,EditProductsViewModel>(editCountyVM, editProductVM);


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow