序列包含多个元素Mvc

时间:2014-02-11 21:46:37

标签: c# mysql asp.net-mvc

问题功能 以下是我遇到问题的功能。我一直得到“序列包含多个元素”错误。这是假设的。但是,我不确定如何返回信息,以便我可以使用它。任何帮助,将不胜感激。

EditCountyViewModel是一个包含public County county公共List CountyList . I have also tried changing the Read<> to Read`的小类,它只是我所有县信息的基类。

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&amp; products表。

1 个答案:

答案 0 :(得分:24)

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