问题描述
我具有以下模型结构。
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);
}
我的数据库表结构如下:
-----------------------------------------------------------------
| Id | Name |Metadata |
| | | |
| 1 | Bhushan |{"houseName": 'ABC',"BikeName": "VC"} |
-----------------------------------------------------------------
//上Db.Select(query)时出错;
The multi-part identifier "InfoMetadata .BikeName " Could not be bound.'
有人可以告诉我如何解析这类数据。
解决方法
在OrmLite中,默认情况下会使用它们的configured Complex Type Serializer来对复杂类型进行blob,您通常无法对它们进行服务器端查询,因此您不应对希望使用SQL进行查询的字段进行blob,因为它们只能在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'");
否则,您将只能在执行服务器查询后使用C#中的LINQ在客户端上查询结果,例如:
var results = Db.Select(Db.From<Information>().Where(x => x.Name == query));
var filteredResults = results.Where(x => x.InfoMetadata.BikeName == query).ToList();