我有ASP.Net Core
作为datbase的C#
(DynamoDb
)应用程序下面是我的函数外观:-
public async Task<object> GetEvent(string eventId)
{
try
{
var req = new QueryRequest()
{
TableName = _tableName,
KeyConditionExpression = "EventId = :v_Id",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
{
{ ":v_Id", new AttributeValue()
{
S = eventId
} },
{
":v_isDeletedFlag",new AttributeValue()
{
BOOL = false
}
}
},
FilterExpression = "IsDeleted = :v_isDeletedFlag",
Limit = 1,
};
var res = await _dynamoClient.QueryAsync(req);
if (res.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
return res.Items;
}
else
{
throw new Exception("No such event found");
}
}
catch (Exception ex)
{
throw;
}
}
但是上述方法返回的对象如下:-
[
{
"CreatedOn": {
"b": null,
"bool": false,
"isBOOLSet": false,
"bs": [],
"l": [],
"isLSet": false,
"m": {},
"isMSet": false,
"n": null,
"ns": [],
"null": false,
"s": "Friday,16 August 2019 14.33.35",
"ss": []
},
"IsAlcoholAllowed": {
"b": null,
"bool": false,
"isBOOLSet": true,
"bs": [],
"l": [],
"isLSet": false,
"m": {},
"isMSet": false,
"n": null,
"ns": [],
"null": false,
"s": null,
"ss": []
},
"Description": {
"b": null,
"bool": false,
"isBOOLSet": false,
"bs": [],
"l": [],
"isLSet": false,
"m": {},
"isMSet": false,
"n": null,
"ns": [],
"null": false,
"s": "Hello Events",
"ss": []
},
"Title": {
"b": null,
"bool": false,
"isBOOLSet": false,
"bs": [],
"l": [],
"isLSet": false,
"m": {},
"isMSet": false,
"n": null,
"ns": [],
"null": false,
"s": "Test Event Mumbai",
"ss": []
}}]
但是我想要的是具有以下键和值的对象
[
{
"CreatedOn": "Friday,16 August 2019 14.33.35",
"IsAlcoholAllowed": false,
"Description": "Hello Events"
}]
我怎么能得到这个?
谢谢!
答案 0 :(得分:0)
如果我正确理解了您的问题, 您正在使用Dynamo的Low-Level编程模型查询数据,这就是为什么要获取描述每个属性的冗长JSON的原因。
如果您想更好地控制响应,那么如何使用DynamoDB的Object-Persistence编程模型?
定义代表您的文档的模型:
[DynamoDBTable("Events")]
public class Event
{
[DynamoDBHashKey]
public int EventId { get; set; }
public DateTime CreatedOn { get; set; }
public bool IsAlcoholAllowed { get; set; }
public string Description { get; set; }
public bool IsDeleted { get; set; }
}
然后通过以下方式查询:
var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var filter = new QueryFilter();
filter.AddCondition(Model.HashKey, QueryOperator.Equal, 123);
var events = await context.FromQueryAsync<Event>(new QueryOperationConfig
{
Select = SelectValues.AllAttributes,
Filter = filter
});
然后您可以在JSON解析器中传递此模型的列表,从而使您更接近目标数据结构。