使用“PetaPoco”ORM“选择”查询

时间:2016-09-08 11:31:00

标签: c# .net petapoco

我尝试了类似下面的内容,但它没有用。

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 个答案:

答案 0 :(得分: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);
    }