How do you write a parameterized where-in raw sql query in Entity Framework

后端 未结 3 1696
忘掉有多难
忘掉有多难 2020-12-15 05:30

How do you write a parameterized where-in raw sql query in Entity Framework? I\'ve tried the following:

string dateQueryString = String.Join(\",\", chartMode         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 06:30

    Here's how you would write your query in SQL.

    select *
        from MyTable
        where dateColumn in ('2014-01-01', '2014-02-01', '2014-03-01')
    

    So one shall not expect otherwise than having to represent this string entirely delimited by parenthesis.

    var dateQueryString = string.Join(",", chartModelData.GetFormattedDateList());
    // Dates shall be returned as DateTime.ToShortDateTimeString() as follows:
    // '2014-01-01', '2014-02-01', '2014-03-01'
    

    Then only remains to wrap it in parenthesis.

    var sql = @"select max(data_seq)    as MaxSeq
                        , min(data_seq) as MinSeq
                        , count(1)      as TotSampleCnt
                    from spcdata_tb
                    where data_wadate in (@DateParam)
                        and line_code  =  @LineCode
                        and model_no   =  @ModelNumber
                        and lot_no     =  @LotNumber
                        and equip_no like @EquipNumber";
    

    Provide the parameters values for each named parameter, and voilà! This shall do it!

提交回复
热议问题