I have below query returning some json result in postgres
select t.transaction_id,
jsonb_agg(jsonb_build_object('title', t.title, 'created_date', t.created_date, 'created_by', t.created_by))
AS content
from temp_table group by t.transaction_id
I have below c# code
public class MainClauseHistoryDto
{
[JsonProperty("transaction_id")]
public int TransactionId { get; set; }
[JsonProperty("content")]
public List<NegotiatedContent> NegotiatedContents { get; set; }
}
public class NegotiatedContent
{
[JsonProperty("created_by")]
public string CreatedBy { get; set; }
[JsonProperty("created_date")]
public DateTime CreatedDate { get; set; }
[JsonProperty("title")]
public string Title{ get; set; }
}
I would like to bind above json to NegotiatedContent. In my DataAccess layer i am calling stored procedure and returning above data as DataTable.
//below code to query db and return dataset.
var result = await _negotiationRepository.GetMainClauseHistoryAsync(IndicationRequestId, ParagraphId);
var mainClauseHistory = result.Tables["main_clause_history_tbl"].ToModelList<MainClauseHistoryDto>();
Below is ToModelList
public static List<T> ToModelList<T>(this DataTable dt)
{
if(dt?.Rows.Count > 0)
{
var seralizeData = JsonConvert.SerializeObject(dt);
return JsonConvert.DeserializeObject<List<T>>(seralizeData)!;
}
else
return new List<T>();
}
In the above code I am getting error
Error converting value "[{"created_by": "iser1"
Above error message with whole json is coming, may I know how we can bind json directly to our c# model? Any help would be appreciated. Thanks