2016-09-08 41 views
1

我嘗試了類似以下內容,但它沒有奏效。「選擇」查詢使用「PetaPoco」ORM

var _records = new string[] {"SqlServer", "IIS" };

var result = db.Fetch<EntityRecords>(@" select * from tblRecords where RecordName IN rs", new { rs = _records });

,也是我試圖像下面的另一種方式,但同樣的問題

var _records = new string[] {"SqlServer", "IIS" };

var query = PetaPoco.Sql.Builder.Select("*").From("tblRecords").Where("RecordName IN (@rs)",new { rs = _records }); 
    var result = db.Query<EntityRecords>(query); 

回答

1

第一個應該是

var result = db.Fetch<EntityRecords>(@" select * from tblRecords where RecordName IN (@rs)", new { rs = _records }); 

var result = db.Fetch<EntityRecords>(@" select * from tblRecords where RecordName IN (@0)", _records); 

第二個我不太確定,因爲下面的測試通過

[Fact] 
    public void Append_GivenArrayAndValue_ShouldBeValid() 
    { 
     // Simple collection parameter expansion 
     _sql = Sql.Builder.Append("@0 IN (@1) @2", 20, new int[] { 1, 2, 3 }, 30); 

     _sql.SQL.ShouldBe("@0 IN (@1,@2,@3) @4"); 
     _sql.Arguments.Length.ShouldBe(5); 
     _sql.Arguments[0].ShouldBe(20); 
     _sql.Arguments[1].ShouldBe(1); 
     _sql.Arguments[2].ShouldBe(2); 
     _sql.Arguments[3].ShouldBe(3); 
     _sql.Arguments[4].ShouldBe(30); 
    } 

    [Fact] 
    public void Append_GivenArrayAndNamedValue_ShouldBeValid1() 
    { 
     // Simple collection parameter expansion 
     _sql = Sql.Builder.Append("@p1 IN (@p2) @p3", new { p1 = 20 }, new { p2 = new int[] { 1, 2, 3 }}, new { p3 = 30 }); 

     _sql.SQL.ShouldBe("@0 IN (@1,@2,@3) @4"); 
     _sql.Arguments.Length.ShouldBe(5); 
     _sql.Arguments[0].ShouldBe(20); 
     _sql.Arguments[1].ShouldBe(1); 
     _sql.Arguments[2].ShouldBe(2); 
     _sql.Arguments[3].ShouldBe(3); 
     _sql.Arguments[4].ShouldBe(30); 
    } 
+0

第一個例子它的工作好,thanx的幫助我 – yats