簡體   English   中英

如何返回 FirstOrDefault

[英]How to return FirstOrDefault

我有方法只想返回一條記錄,但我不能使用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;
}

Model:

public class CorridoioWebResponse
{
    public string stab { get; set; }
    public string maga { get; set; }
    public string area { get; set; }
    public Int32 cors { get; set; }
}

它給我的錯誤:

無法將類型“System.Collections.Generic.IEnumerable<ModelSOme.Model_WebInterface.Mapatura.CorridoioWebResponse>”隱式轉換為“ModelSomeModel_WebInterface.Mapatura.CorridoioWebResponse”。

此代碼有兩個問題 - 用於生成查詢的字符串連接以及在該方法僅返回單個項目時返回IEnumerable<>結果。

現在,你得到一個編譯錯誤。 即使它已修復,當文本值被注入查詢時,您也會遇到運行時錯誤。 子字符串周圍沒有空格,因此整個 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});

這將按名稱傳遞參數值並返回第一個結果

問題是await cn.QueryAsync<CorridoioWebResponse>(strsql)返回IEnumerable<CorridoioWebResponse>並且您嘗試將其分配給CorridoioWebResponse coridoio並獲得類型不匹配錯誤。

嘗試這樣的事情: coridoio = _corridoio.FirstOrDefault(); (不要忘記using System.Linq

我看到錯誤:

ModelSOme.Model_WebInterface是不同的ModelSomeModel_WebInterface

你看 ”。” 在 S0me.Model 和 _corridoio 之間,我想這是一個列表

您應該使用相同的 model。

似乎更像是 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)總是返回一個IEnumerable ,因為QueryAsync與 SQL 表有關系,認為 SQL 表就像一個IEnumerable列表。

您的對象( coridoio )類型應該是IEnumerable<CorridoioWebResponse>然后您可以使用coridoio.FirstOrDefault()並返回它。

如果您不能使用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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM