简体   繁体   English

ServiceStack ORMLite 如何在查询中 fparse JSON 数据

[英]ServiceStack ORMLite How to fparse JSON data in Query

I have the following model structure.我有以下 model 结构。

public class Inforamation 
{ 
  public Guid Id { get; set; }

  public string Name { get; set; }

  public InfoMetadata Metadata { get; set; }
}

public class InfoMetadata 
{ 
  // Bike Info

  public string BikeName { get; set; }
  public string ModelNumber { get; set; }

  // House Info
  public string HouseName { get; set; }
  public string HouseNumber { get; set; }

}

Request DTO请求DTO

public class RequestDto
{ 
public string Query { get; set; }
}

//Service //服务

 public void Get(RequestDto request)
    {
      var query = Db.From<Inforamation>();
    
      query = query.And<Inforamation>(v => v.Name == query || v.InfoMetadata.BikeName == query);

var result = Db.select(query);
    
    }

My database table structure is like:我的数据库表结构是这样的:

-----------------------------------------------------------------
|   Id      |   Name    |Metadata                               |
|           |           |                                       |
|      1    |  Bhushan  |{"houseName": 'ABC', "BikeName": "VC"} |
-----------------------------------------------------------------

//Error getting on Db.Select(query); //获取 Db.Select(query) 时出错;

The multi-part identifier "InfoMetadata .BikeName " could not be bound.'

Can some one please told me how to parse that type of data.有人能告诉我如何解析这种类型的数据吗?

Complex Types are blobbed by default in OrmLite using their configured Complex Type Serializer which you typically can't perform server side queries on, so you shouldn't blob fields you want to be able to query with SQL as they can only be inspected after the resultsets are materialized in C#.在 OrmLite 中,复杂类型默认使用其配置的复杂类型序列化程序进行处理,您通常无法对其执行服务器端查询,因此您不应该使用 SQL 对您希望能够查询的字段进行处理,因为它们只能在结果集在 C# 中具体化。

If you're using PostgreSQL you can use its rich JSON Data Type by annotating Complex Type properties with [PgSqlJson] or [PgSqlJsonB] attributes, eg:如果您使用的是PostgreSQL ,则可以通过使用[PgSqlJson][PgSqlJsonB]属性注释复杂类型属性来使用其丰富的 JSON 数据类型,例如:

public class TableJson
{
    public int Id { get; set; }

    [PgSqlJson]
    public ComplexType ComplexTypeJson { get; set; }

    [PgSqlJsonB]
    public ComplexType ComplexTypeJsonb { get; set; }
}

db.Insert(new TableJson
{
    Id = 1,
    ComplexTypeJson = new ComplexType {
        Id = 2, SubType = new SubType { Name = "JSON" }
    },
    ComplexTypeJsonb = new ComplexType {
        Id = 3, SubType = new SubType { Name = "JSONB" }
    },
});

Where they can then be queried on the server with PostgreSQL's JSON SQL Syntax and functions :然后可以使用PostgreSQL 的 JSON SQL 语法和函数在服务器上查询它们:

var result = db.Single<TableJson>("table_json->'SubType'->>'Name' = 'JSON'");

Otherwise you will only be able to query the results on the client using LINQ in C# after executing the Server query, eg:否则你将只能在执行Server查询后使用LINQ in C#在客户端查询结果,eg:

var results = Db.Select(Db.From<Information>().Where(x => x.Name == query));
var filteredResults = results.Where(x => x.InfoMetadata.BikeName == query).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM