联表查询时,isJson=true的字段,select出来是null 返回
SqlSugar
沟通中
2
225
悬赏:0 飞吻
namespace demo;
[SugarTable("project")]
public class SysProjectDTO
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int id { get; set; }
public string name { get; set; }
public int organ_id { get; set; }
public int status { get; set; }
public int data_type { get; set; }
[SugarColumn(ColumnDataType = "jsonb", IsJson = true)]
public JsonDataModel json_data { get; set; }
}namespace demo;
[SugarTable("project_combination")]
public class SysProjectCombinationDTO
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int id { get; set; }
public int project_id { get; set; }
public string name { get; set; }
public decimal unit_price { get; set; }
public decimal discount { get; set; }
public int status { get; set; }
}namespace demo;
public class JsonDataModel
{
public int no { get; set; }
public string note { get; set; }
public string note2 { get; set; }
}namespace demo;
public class alldto
{
public int cpc_id { get; set; }
public string combination_name { get; set; }
public int project_id { get; set; }
public string project_name { get; set; }
public decimal unit_price { get; set; }
public decimal discount { get; set; }
public JsonDataModel json_data { get; set; }
}上面是类,是json_data我处理的不对吗?
单独查:
var project = db.Sugardb.Queryable<SysProjectDTO>().ToList();
结果是:
[
{
"id": 1,
"name": "Project A",
"organ_id": 100,
"status": 1,
"data_type": 0,
"json_data": {
"no": 1,
"note": "Note A",
"note2": "Note A2"
}
}
]然后关联查询:
var joindata = db.Sugardb.Queryable<SysProjectCombinationDTO>()
.LeftJoin<SysProjectDTO>((spc, p) => spc.project_id == p.id)
.Select((spc, p) => new alldto()
{
cpc_id = spc.id,
project_id = p.id,
project_name = p.name,
combination_name = spc.name,
unit_price = spc.unit_price,
discount = spc.discount,
json_data = p.json_data
})
.ToList();结果是:
[
{
"cpc_id": 0,
"combination_name": "Combination A1",
"project_id": 1,
"project_name": "Project A",
"unit_price": 100,
"discount": 0.9,
"json_data": null
},
{
"cpc_id": 0,
"combination_name": "Combination A2",
"project_id": 1,
"project_name": "Project A",
"unit_price": 200,
"discount": 0.85,
"json_data": null
}
]只能先在实体类设置成isJson=true的string类型字段,之后再手动转换吗?
这是demo
热忱回答(2)
-
FELIX VIP0
1个月前alldto中的json_data也需要加[SugarColumn(isJson = true)]
0 回复 -
国王迷宫 VIP0
1个月前@FELIX:解决了,感谢,加了之后就可以了
0 回复