ServiceStack ORMLite 如何在查询中 fparse JSON 数据
ServiceStack ORMLite How to fparse JSON data in Query
我有以下模型结构。
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; }
}
请求 DTO
public class RequestDto
{
public string Query { get; set; }
}
//服务
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);
}
我的数据库table结构如下:
-----------------------------------------------------------------
| Id | Name |Metadata |
| | | |
| 1 | Bhushan |{"houseName": 'ABC', "BikeName": "VC"} |
-----------------------------------------------------------------
//获取Db.Select(查询)时出错;
The multi-part identifier "InfoMetadata .BikeName " could not be bound.'
谁能告诉我如何解析这种类型的数据。
复杂类型在 OrmLite 中默认使用它们的 configured Complex Type Serializer 进行 blob,您通常无法对其执行服务器端查询,因此您不应该 blob 您希望能够使用 [=28 查询的字段=] 因为只有在结果集在 C# 中具体化后才能检查它们。
如果您正在使用 PostgreSQL,您可以 use its rich JSON Data Type 通过使用 [PgSqlJson]
或 [PgSqlJsonB]
注释复杂类型属性属性,例如:
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" }
},
});
然后可以在服务器上使用 PostgreSQL's JSON SQL Syntax and functions:
查询它们
var result = db.Single<TableJson>("table_json->'SubType'->>'Name' = 'JSON'");
否则你将只能在执行Server查询后使用C#中的LINQ在客户端查询结果,例如:
var results = Db.Select(Db.From<Information>().Where(x => x.Name == query));
var filteredResults = results.Where(x => x.InfoMetadata.BikeName == query).ToList();
我有以下模型结构。
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; }
}
请求 DTO
public class RequestDto
{
public string Query { get; set; }
}
//服务
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);
}
我的数据库table结构如下:
-----------------------------------------------------------------
| Id | Name |Metadata |
| | | |
| 1 | Bhushan |{"houseName": 'ABC', "BikeName": "VC"} |
-----------------------------------------------------------------
//获取Db.Select(查询)时出错;
The multi-part identifier "InfoMetadata .BikeName " could not be bound.'
谁能告诉我如何解析这种类型的数据。
复杂类型在 OrmLite 中默认使用它们的 configured Complex Type Serializer 进行 blob,您通常无法对其执行服务器端查询,因此您不应该 blob 您希望能够使用 [=28 查询的字段=] 因为只有在结果集在 C# 中具体化后才能检查它们。
如果您正在使用 PostgreSQL,您可以 use its rich JSON Data Type 通过使用 [PgSqlJson]
或 [PgSqlJsonB]
注释复杂类型属性属性,例如:
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" }
},
});
然后可以在服务器上使用 PostgreSQL's JSON SQL Syntax and functions:
查询它们var result = db.Single<TableJson>("table_json->'SubType'->>'Name' = 'JSON'");
否则你将只能在执行Server查询后使用C#中的LINQ在客户端查询结果,例如:
var results = Db.Select(Db.From<Information>().Where(x => x.Name == query));
var filteredResults = results.Where(x => x.InfoMetadata.BikeName == query).ToList();