Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 序列包含多个元素Mvc_C#_Mysql_Asp.net Mvc - Fatal编程技术网

C# 序列包含多个元素Mvc

C# 序列包含多个元素Mvc,c#,mysql,asp.net-mvc,C#,Mysql,Asp.net Mvc,问题函数 下面是我遇到问题的函数。我不断得到“序列包含多个元素”错误。应该是这样的。但是,我不确定如何返回信息以便使用。任何帮助都将不胜感激 EditCountyViewModel是一个小类,它包含public County-Countypublic List CountyList。我还尝试将Read改为Read`这只是我所有县信息的基类 public EditCountyViewModel FindByCounty(string countyName) {

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

EditCountyViewModel
是一个小类,它包含
public County-County
public List CountyList
。我还尝试将Read改为
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();

        }
public EditCountyViewModel FindByCounty(字符串countyName)
{
var参数=新的DynamicParameters();
参数.Add(“@CountyName”,值:CountyName);
var query=@“选择countries.id
,countryName
,县。网站
,各县,地址
,县,市
,县,州
,countries.PhonePrimary
,countries.PhoneAlt
,countries.RecordsOnline
,countries.BackToYear
,县。成本
,products.ProductName
,产品名称
,countyproduct.TurnTime\u MinHours
,countyproduct.TurnTime\u最大小时数
,countyproduct.价格
从…起
县、县产品
哪里
CountryName=@CountyName和countyproduct.countiesID=countyproduct.countiesID和countyproduct.productsID=products.ID;“;
//使用(var multi=this.db.QueryMultipl(query,new{countyName}))
//{
//EditCountyViewModel editVM=新的EditCountyViewModel();
//editVM.county=multi.Read().Single();
//返回editVM;
//}
返回此.db.Query(查询,参数).SingleOrDefault();
}

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

SingleOrDefault()
确保只返回1条记录,如果有更多记录,将抛出。如果您只想获取第一个错误,请使用
FirstOrDefault()

哪一行给出了该错误?返回行。但是在注释掉的代码中,
editVM.county=multi.Read().Single()给了我一个错误,sql看起来很有趣。countyproduct.countiesID=countyproduct.countiesID是有意的吗?为什么会被否决?这是正确的答案。如果需要多个元素,请不要使用
Single
SingleOrDefault
。如果只需要第一个,请使用
first
first或default