序列包含多个元素 Mvc



>问题函数下面是我遇到问题的函数。我不断收到"序列包含多个元素"错误。这是应该的。但是,我不确定如何返回信息以便我可以使用它。任何帮助将不胜感激。

EditCountyViewModel是一个小类public County county其中包含一个公共列表县列表. I have also tried changing the Read<> to读取",它只是我所有县信息的基类。

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();
        }

我想我需要另一个类来处理来自 countyproduct & products 表的项目。

SingleOrDefault()确保只返回 1 条记录,如果有更多记录,则会抛出。如果您只想抓住第一个,请使用FirstOrDefault()

最新更新