ServiceStack Ormlite Deserialize Array for In Clause

I am storing some query criteria in the db via a ToJson() on the object that contains all the criteria. A simplified example would be:

{"FirstName" :[ {Operator: "=", Value: "John"}, { Operator: "in", Value:"    ["Smith", "Jones"]"}], "SomeId": [Operator: "in", Value: "[1,2,3]" }]}

The lists are either string, int, decimal or date. These all map to the same class/table so it is easy via reflection to get FirstName or SomeId 's type.

I'm trying to create a where clause based on this information:

 if (critKey.Operator == "in")
 {
   wb.Values.Add(keySave + i, (object)ConvertList<Members>(key, 
  (string)critKey.Value));
   wb.WhereClause = wb.WhereClause + " And {0} {1} (@{2})".Fmt(critKey.Column, 
  critKey.Operator, keySave + i);
}
else
{
  wb.Values.Add(keySave + i, (object)critKey.Value);
  wb.WhereClause = wb.WhereClause + " And {0} {1} @{2}".Fmt(critKey.Column, critKey.Operator, keySave + i);
}

It generates something like this (example from my tests, yes I know the storenumber part is stupid):

  Email = @Email0 And StoreNumber = @StoreNumber0 And StoreNumber in (@StoreNumber1)

I'm running into an issue with the lists. Is there a nice way to do this with any of the ormlite tools instead of doing this all by hand? The where clause generates fine except when dealing with lists. I'm trying to make it generic but having a hard time on that part.

Second question maybe related but I can't seem to find how to use parameters with in . Coming from NPoco you can do (colum in @0 , somearray)` but I cant' seem to find out how to do this without using Sql.In.


处理列表时,可以使用下面的示例

var storeNumbers = new [] { "store1", "store2", "store3" };
var ev = Db.From<MyClass>
    .Where(p => list.Contains(p => p.StoreNumber));
var result = Db.Select(ev);

I ended up having to write my own parser as it seems ormlite doesn't support have the same support for query params for lists like NPoco. Basically I'd prefer to be able to do this:

Where("SomeId in @Ids") and pass in a parameter but ended up with this code:

 listObject = ConvertListObject<Members>(key, (string)critKey.Value);
 wb.WhereClause = wb.WhereClause + " And {0} {1} ({2})"
   .Fmt(critKey.Column, critKey.Operator,listObject.EscapedList(ColumnType<Members>(key)));

 public static string EscapedList(this List<object> val, Type t)
 {
    var escapedList = "";
    if (t == typeof(int) || t == typeof(float) || t == typeof(decimal))
    {
       escapedList = String.Join(",", val.Select(x=>x.ToString()));
    } else
    {
      escapedList = String.Join(",", val.Select(x=>"'" + x.ToString() + "'"));
    }
    return escapedList;

  }

I'd like to see other answers especially if I'm missing something in ormlite.

链接地址: http://www.djcxy.com/p/65980.html

上一篇: ORMLite / Servicestack:一起连接多个嵌套表

下一篇: 用于In子句的ServiceStack Ormlite反序列化数组