簡體   English   中英

如何將列表添加到SQL查詢?

[英]How to add list to SQL query?

我傳遞參數month並追加到列表中。

如何將列表添加到SqlQuery參數?

錯誤:從對象類型System.Collections.Generic.List`1 []到已知托管提供程序本機類型的映射不存在。

public ActionResult filterhre(int? month, int? year)
{
    List<int> lst = new List<int>();

    for (int i = 1; i <= @month; i++)
    {
        lst.Add(i);
    }

    ViewBag.location_fraud_year = db.Database.SqlQuery<YearCheck>(@"SELECT fraud_location, count(claim_amount) as counting_claim, sum(claim_amount) as counting_sum FROM testtable
where month(datelocation) in ({0}) and year(datelocation)={1} and fraud_location is not null ",lst, year).ToList(); 
}

模型:

public class YearCheck
{
    public string fraud_location { get; set; }
    public int? counting_claim { get; set; }
    public decimal? counting_sum { get; set; }
}

錯誤消息告訴您,.Net不知道如何在SQL中將列表轉換為逗號分隔的列表。 您必須自己做:

ViewBag.location_fraud_year = db.Database.SqlQuery<YearCheck>(@"...", String.Join(",", names.ToArray()), year).ToList();

暫無
暫無

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

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