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
。