如何 return FirstOrDefault
How to return FirstOrDefault
我有方法想 return 只有一条记录,但我不能使用 FirstOrDefault
。与数据库的连接正常,我已经试过了。
我想查找传递一些值的记录。
这是我试过的:
public async Task<CorridoioWebResponse> GetCorridoiWeb_ID(string stab, string maga, string area, Int32 cors)
{
string strsql = string.Empty;
string strErrore = string.Empty;
string strConnessione = _configuration.GetConnectionString("ConnDB");
SqlServerCompiler compiler = new SqlServerCompiler();
CorridoioWebResponse coridoio;
using (IDbConnection cn = new SqlConnection(strConnessione))
{
using (var db = new QueryFactory(cn, compiler))
{
strsql = "SELECT * from tmhit.CORRIDOI_CORSIE WHERE STAB=" + stab + "AND MAGA =" + maga + "AND AREA=" + area + "AND CORS=" + cors;
//strsql = "SELECT top 1 STAB, MAGA, AREA, CORS from tmhit.CORRIDOI_CORSIE WHERE STAB=" + stab + "AND MAGA =" + maga + "AND AREA=" + area + "AND CORS=" + cors;
//SELECT top 1 STAB, MAGA, AREA, CORS from tmhit.CORRIDOI_CORSIE WHERE STAB = 1 and MAGA = 0 and AREA = 0 and CORS = 9
var _corridoio = await cn.QueryAsync<CorridoioWebResponse>(strsql);
if (_corridoio.Count() == 0)
throw new ToyotaException("Non ci sono corridoi mappati");
coridoio = _corridoio;
}
}
return coridoio;
}
型号:
public class CorridoioWebResponse
{
public string stab { get; set; }
public string maga { get; set; }
public string area { get; set; }
public Int32 cors { get; set; }
}
它给我的错误:
Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<ModelSOme.Model_WebInterface.Mapatura.CorridoioWebResponse>' to
'ModelSomeModel_WebInterface.Mapatura.CorridoioWebResponse'.
我看到错误:
ModelSOme.Model_WebInterface是区别ModelSomeModel_WebInterface
你看到“。”在S0me.Model和_corridoio之间,是一个列表,我以为
你应该使用相同的模型。
似乎更像是一个 SQL 问题而不是 C# 问题。
试试这个:
SELECT Top 1 * from tmhit.CORRIDOI_CORSIE WHERE STAB=.....
编辑:
改变这个:
var _corridoio = await cn.QueryAsync<CorridoioWebResponse>(strsql);
对此:
var _corridoioList = await cn.QueryAsync<IEnumerable<CorridoioWebResponse>>(strsql);
var _corridoio = _corridoioList.FirstOrDefault()
问题是 await cn.QueryAsync<CorridoioWebResponse>(strsql)
returns IEnumerable<CorridoioWebResponse>
并且您试图将其分配给 CorridoioWebResponse coridoio
并出现类型不匹配错误。
尝试这样的事情:coridoio = _corridoio.FirstOrDefault();
(别忘了 using System.Linq
)
此代码有两个问题 - 生成查询的字符串连接和 returning IEnumerable<>
结果,当方法 return 只有一个项目时。
现在,您遇到编译错误。即使它是固定的,当文本值被注入到查询中时,您也会得到 runtime 错误。子字符串周围没有空格,因此整个 WHERE 子句将无效:WHERE STAB=123AND MAGA =345AND CORS=abc
将代码替换为:
var strsql = "SELECT TOP 1 * from tmhit.CORRIDOI_CORSIE WHERE STAB=@stab AND MAGA=@maga AND AREA=@area AND CORS=@cors";
var _corridoio = await connection.QuerySingleOrDefaultAsync<CorridoioWebResponse>(
strsql,
new {stab,maga,area,cors});
这将按名称传递参数值,return 第一个结果
await cn.QueryAsync<CorridoioWebResponse>(strsql)
总是return是IEnumerable
,因为QueryAsync
与SQL有关系 table,认为,SQL tables 就像一个 IEnumerable
列表。
您的对象的 (coridoio
) 类型应该是 IEnumerable<CorridoioWebResponse>
然后您可以使用 coridoio.FirstOrDefault()
和 return this.
如果您不能使用 FirstOrDefault()
则使用 ToList()
:
var _corridoio = (await cn.QueryAsync<CorridoioWebResponse>(strsql)).ToList();
if (_corridoio.Count == 0)
throw new ToyotaException("Non ci sono corridoi mappati");
coridoio = _corridoio[0];
我有方法想 return 只有一条记录,但我不能使用 FirstOrDefault
。与数据库的连接正常,我已经试过了。
我想查找传递一些值的记录。
这是我试过的:
public async Task<CorridoioWebResponse> GetCorridoiWeb_ID(string stab, string maga, string area, Int32 cors)
{
string strsql = string.Empty;
string strErrore = string.Empty;
string strConnessione = _configuration.GetConnectionString("ConnDB");
SqlServerCompiler compiler = new SqlServerCompiler();
CorridoioWebResponse coridoio;
using (IDbConnection cn = new SqlConnection(strConnessione))
{
using (var db = new QueryFactory(cn, compiler))
{
strsql = "SELECT * from tmhit.CORRIDOI_CORSIE WHERE STAB=" + stab + "AND MAGA =" + maga + "AND AREA=" + area + "AND CORS=" + cors;
//strsql = "SELECT top 1 STAB, MAGA, AREA, CORS from tmhit.CORRIDOI_CORSIE WHERE STAB=" + stab + "AND MAGA =" + maga + "AND AREA=" + area + "AND CORS=" + cors;
//SELECT top 1 STAB, MAGA, AREA, CORS from tmhit.CORRIDOI_CORSIE WHERE STAB = 1 and MAGA = 0 and AREA = 0 and CORS = 9
var _corridoio = await cn.QueryAsync<CorridoioWebResponse>(strsql);
if (_corridoio.Count() == 0)
throw new ToyotaException("Non ci sono corridoi mappati");
coridoio = _corridoio;
}
}
return coridoio;
}
型号:
public class CorridoioWebResponse
{
public string stab { get; set; }
public string maga { get; set; }
public string area { get; set; }
public Int32 cors { get; set; }
}
它给我的错误:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<ModelSOme.Model_WebInterface.Mapatura.CorridoioWebResponse>' to 'ModelSomeModel_WebInterface.Mapatura.CorridoioWebResponse'.
我看到错误:
ModelSOme.Model_WebInterface是区别ModelSomeModel_WebInterface
你看到“。”在S0me.Model和_corridoio之间,是一个列表,我以为
你应该使用相同的模型。
似乎更像是一个 SQL 问题而不是 C# 问题。
试试这个:
SELECT Top 1 * from tmhit.CORRIDOI_CORSIE WHERE STAB=.....
编辑:
改变这个:
var _corridoio = await cn.QueryAsync<CorridoioWebResponse>(strsql);
对此:
var _corridoioList = await cn.QueryAsync<IEnumerable<CorridoioWebResponse>>(strsql);
var _corridoio = _corridoioList.FirstOrDefault()
问题是 await cn.QueryAsync<CorridoioWebResponse>(strsql)
returns IEnumerable<CorridoioWebResponse>
并且您试图将其分配给 CorridoioWebResponse coridoio
并出现类型不匹配错误。
尝试这样的事情:coridoio = _corridoio.FirstOrDefault();
(别忘了 using System.Linq
)
此代码有两个问题 - 生成查询的字符串连接和 returning IEnumerable<>
结果,当方法 return 只有一个项目时。
现在,您遇到编译错误。即使它是固定的,当文本值被注入到查询中时,您也会得到 runtime 错误。子字符串周围没有空格,因此整个 WHERE 子句将无效:WHERE STAB=123AND MAGA =345AND CORS=abc
将代码替换为:
var strsql = "SELECT TOP 1 * from tmhit.CORRIDOI_CORSIE WHERE STAB=@stab AND MAGA=@maga AND AREA=@area AND CORS=@cors";
var _corridoio = await connection.QuerySingleOrDefaultAsync<CorridoioWebResponse>(
strsql,
new {stab,maga,area,cors});
这将按名称传递参数值,return 第一个结果
await cn.QueryAsync<CorridoioWebResponse>(strsql)
总是return是IEnumerable
,因为QueryAsync
与SQL有关系 table,认为,SQL tables 就像一个 IEnumerable
列表。
您的对象的 (coridoio
) 类型应该是 IEnumerable<CorridoioWebResponse>
然后您可以使用 coridoio.FirstOrDefault()
和 return this.
如果您不能使用 FirstOrDefault()
则使用 ToList()
:
var _corridoio = (await cn.QueryAsync<CorridoioWebResponse>(strsql)).ToList();
if (_corridoio.Count == 0)
throw new ToyotaException("Non ci sono corridoi mappati");
coridoio = _corridoio[0];