i have a table in my database that stores translations
ResourseKey | ResourseText | LanguageId |
---|---|---|
Home | Home | 1 |
Home | Hem | 2 |
Search | Search | 1 |
Search | Sök | 2 |
and the Languages table
Id | Language |
---|---|
1 | English |
2 | Swedish |
I am trying to return an object that looks like this
{
"Home":{
"Swedish":"Hem",
"English":"Home"
},
"Search":{
"Swedish":"Sök",
"English":"Search"
}
}
i started by doing
var data = await _context.Translations
.Include(x => x.Language)
.ToListAsync();
return data.ToLookup(p => p.ResourseKey , p => p).ToDictionary(x => x.Key, x => x.ToArray());
but I can’t go any further though
I want to format the lookup phrase to return the desired structure instead of returning just p
I can off course fix it by js on the client side but I want to all the heavy lift on the server
I’m really sorry if my question is unstructured this is the first time a post a question on SOF
2
Answers
I think Inner Join can help you in this kind of scenario.
Consider there is two Entity as per below:
And the other:
Now we initialize some data for inner join and select the data that we need:
To format the json you can use the JsonConverter class, the write method is the method that generate the output. The parameter writer has many methods that helps you to generate the output just as you want.
and:
and (on a console application):
will generate this output:
if you are using aspnet, then register the converter by adding this code on your services.AddController line (that can be on Program.cs or Startup.cs):
then use:
and aspnet you do the job.