Tuesday, 10 September 2013

Serve Key: "Key" & Value: "Value" as Key: Value

Serve Key: "Key" & Value: "Value" as Key: Value

I have dynamic keys and values that I get from db and then parse with
Newtonsoft Json.NET but I don't know how can I serve them as static ones.
Example
This is what I have
{
"Id": 1,
"IsPublic": false,
"Notes": "",
"Values": [
{
"Key": "1",
"Value": "12.02.1991"
}
]
}
This is what I want
{
"Id": 1,
"IsPublic": false,
"Notes": "",
"Values": [
{
"1": "12.02.1991"
}
]
}
What I have tried
I tried to do it manually inside my query itself but it didn't work since
it's trying to assign the value.
return _db.Archives.Single(x => x.Id == id).Batches.SelectMany(x =>
x.Items).Select(item => new
{
item.Id,
item.IsPublic,
item.Notes,
Values = item.ArchiveFieldValues.Select(value => new
{
/*
This works just fine
Key = value.ArchiveField.Key,
Value = value.Value
*/
// This is what I tried but it does not work
value.ArchiveField.Key = value.Value
})
}).AsQueryable();

No comments:

Post a Comment