Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence contains more than one element Mvc

Problem function Below is the function that i'm having issues with. I keep getting the "sequence contains more than one element" error. Which it is suppose to. However, i'm unsure how to return the information so i can use it. Any help would be appreciated.

The EditCountyViewModel is a small class that contains public County countypublic List CountyList. I have also tried changing the Read<> toRead` which is just the base class for all of my county information.

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 = countyproduct.countiesID AND countyproduct.productsID = products.ID;";

            //using (var multi = this.db.QueryMultipl(query, new { countyName }))
            //{
            //    EditCountyViewModel editVM = new EditCountyViewModel();
            //    editVM.county = multi.Read<County>().Single();
            //    return editVM;
            //}
            return this.db.Query<EditCountyViewModel>(query, parameters).SingleOrDefault();

        }

I think i need another class to handle the items coming from the countyproduct & products table.

like image 924
MaylorTaylor Avatar asked Feb 11 '14 21:02

MaylorTaylor


1 Answers

SingleOrDefault() makes sure there is only 1 record returned and will throw if there are more. Use FirstOrDefault() if you just want to grab the first one.

like image 87
Anthony Chu Avatar answered Nov 07 '22 09:11

Anthony Chu