英文:
How can I reorganize data from a table as a complex C# object?
问题
基于这个表格,我想创建一个类似以下 JSON 结构的 C# 对象:
{
"posts":[
{
"1":[
{
"en":{
"title":"Post 1 title",
"body":"This is the content for the post 1"
}
},
{
"fr":{
"title":"Titre de l'article 1",
"body":"Voici le contenu de l'article 1"
}
}
]
},
{
"2":[
{
"en":{
"title":"Post 2 title",
"body":"This is the content for the post 2"
}
}
]
},
{
"3":[
{
"en":{
"title":"Post 3 title",
"body":"This is the content for the post 3"
}
},
{
"fr":{
"title":"Titre de l'article 3",
"body":"Voici le contenu de l'article 3"
}
},
{
"de":{
"title":"Titel von Artikel 3",
"body":"Lesen Sie den Inhalt von Artikel 3"
}
}
]
}
]
}
如何在 C# 中实现这个目标?
在从 SQL 数据库选择数据后,我会循环遍历每一行,尝试将其添加到一个Dictionary<string, Dictionary<string, Dictionary<string, string>>>
的嵌套字典中,检查键是否存在于字典中。如果存在,我只需分配值。如果不存在,我会添加键,然后分配值,如下所示:
Dictionary<string, Dictionary<string, Dictionary<string, string>>> postsDictionary = new();
foreach (var post in postsList)
{
if (postsDictionary.ContainsKey(post.postId))
{
if (postsDictionary[post.postId].ContainsKey(post.language))
{
if (postsDictionary[post.postId][post.language].ContainsKey(post.name))
{
postsDictionary[post.postId][post.language][post.name] = post.value;
}
else
{
postsDictionary[post.postId][post.language].Add(post.name, post.value);
}
}
else
{
postsDictionary[post.postId].Add(post.language, new Dictionary<string, string>());
postsDictionary[post.postId][post.language].Add(post.name, post.value);
}
}
else
{
postsDictionary.Add(post.postId, new Dictionary<string, Dictionary<string, string>>());
postsDictionary[post.postId].Add(post.language, new Dictionary<string, string>());
postsDictionary[post.postId][post.language].Add(post.name, post.value);
}
}
postsDictionary
组织得相当好,但是**是否有更好的方法来实现这一目标?**创建嵌套的字典似乎有点混乱。
英文:
I have a table containing several references to the same posts, themselves containing several references to the same languages like so :
postId | language | name | value |
---|---|---|---|
1 | en | title | Post 1 title |
1 | en | body | This is the content for the post 1 |
1 | fr | title | Titre de l'article 1 |
1 | fr | body | Voici le contenu de l'article 1 |
2 | en | title | Post 2 title |
2 | en | body | This is the content for the post 2 |
3 | en | title | Post 3 title |
3 | en | body | This is the content for the post 3 |
3 | fr | title | Titre de l'article 3 |
3 | fr | body | Voici le contenu de l'article 3 |
3 | de | title | Titel von Artikel 3 |
3 | de | body | Lesen Sie den Inhalt von Artikel 3 |
Base on this table, I would like to create a C# object which ressemble to this :
{
"posts":[
{
"1":[
{
"en":{
"title":"Post 1 title",
"body":"This is the content for the post 1"
}
},
{
"fr":{
"title":"Titre de l'article 1",
"body":"Voici le contenu de l'article 1"
}
}
]
},
{
"2":[
{
"en":{
"title":"Post 2 title",
"body":"This is the content for the post 2"
}
}
]
},
{
"3":[
{
"en":{
"title":"Post 3 title",
"body":"This is the content for the post 3"
}
},
{
"fr":{
"title":"Titre de l'article 3",
"body":"Voici le contenu de l'article 3"
}
},
{
"de":{
"title":"Titel von Artikel 3",
"body":"Lesen Sie den Inhalt von Artikel 3"
}
}
]
}
]
}
How can I achieve that in C# ?
After selecting my datas from my SQL database, I loop through every line, trying to add it to a dictionnary of dictionnary of dictionnary of string Dictionary<string, Dictionary<string, Dictionary<string, string>>>
, testing if the key exists in the dictionnary. If it exists, I just assign the value. If not, I add the key and then assign the value, like so :
Dictionary<string, Dictionary<string, Dictionary<string, string>>> postsDictionnary= new();
foreach(var post in postsList)
{
if (postsDictionnary.ContainsKey(post.postId))
{
if (postsDictionnary[post.postId].ContainsKey(post.language))
{
if (postsDictionnary[post.itemId][post.language].ContainsKey(post.name))
{
postsDictionnary[post.itemId][post.language][post.name] = post.value;
}
else
{
postsDictionnary[post.postId][post.language].Add(post.name, post.value);
}
}
else
{
postsDictionnary[post.postId].Add(post.language, new Dictionary<string, string>());
postsDictionnary[post.postId][post.language].Add(post.name, post.value);
}
}
else
{
postsDictionnary.Add(post.postId, new Dictionary<string, Dictionary<string, string>>());
postsDictionnary[post.postId].Add(post.language, new Dictionary<string, string>());
postsDictionnary[post.postId][post.language].Add(post.name, post.value);
}
}
postsDictionnary
is quite well organized, but is there a better way to achieve this ? It seems pretty dirty to create a dictionnary of dictionnary of dictionnary of string...
答案1
得分: 1
假设你有以下类和对象列表:
public class Post
{
public int PostId { get; set; }
public string Language { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
然后你可以编写以下的LINQ查询:
var posts = new List<Post>
{
new Post { PostId = 1, Language = "en", Name = "title", Value = "Post 1 title" },
new Post { PostId = 1, Language = "en", Name = "body", Value = "This is the content for the post 1" },
new Post { PostId = 1, Language = "fr", Name = "title", Value = "Titre de l'article 1" },
new Post { PostId = 1, Language = "fr", Name = "body", Value = "Voici le contenu de l'article 1" },
new Post { PostId = 2, Language = "en", Name = "title", Value = "Post 2 title" },
new Post { PostId = 2, Language = "en", Name = "body", Value = "This is the content for the post 2" },
new Post { PostId = 3, Language = "en", Name = "title", Value = "Post 3 title" },
new Post { PostId = 3, Language = "en", Name = "body", Value = "This is the content for the post 3" },
new Post { PostId = 3, Language = "fr", Name = "title", Value = "Titre de l'article 3" },
new Post { PostId = 3, Language = "fr", Name = "body", Value = "Voici le contenu de l'article 3" },
new Post { PostId = 3, Language = "de", Name = "title", Value = "Titel von Artikel 3" },
new Post { PostId = 3, Language = "de", Name = "body", Value = "Lesen Sie den Inhalt von Artikel 3" }
};
var result = posts
.GroupBy(x => 1)
.ToDictionary(x => "posts",
p => p.GroupBy(bp => bp.PostId)
.ToDictionary(bp => bp.Key,
bp => bp.GroupBy(lp => lp.Language)
.ToDictionary(lp => lp.Key, lp => lp.ToDictionary(n => n.Name, n => n.Value))
)
);
请注意,这段代码是一个LINQ查询,用于将列表中的Post
对象进行分组和嵌套字典化的操作。如果你需要更多关于这段代码的信息或有任何其他问题,请随时提问。
英文:
Assuming that you have the following class and list of these objects
public class Post
{
public int PostId { get; set; }
public string Language { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
Then you can write the following LINQ Query:
var posts = new List<Post>
{
new Post { PostId = 1, Language = "en", Name = "title", Value = "Post 1 title" },
new Post { PostId = 1, Language = "en", Name = "body", Value = "This is the content for the post 1" },
new Post { PostId = 1, Language = "fr", Name = "title", Value = "Titre de l'article 1" },
new Post { PostId = 1, Language = "fr", Name = "body", Value = "Voici le contenu de l'article 1" },
new Post { PostId = 2, Language = "en", Name = "title", Value = "Post 2 title" },
new Post { PostId = 2, Language = "en", Name = "body", Value = "This is the content for the post 2" },
new Post { PostId = 3, Language = "en", Name = "title", Value = "Post 3 title" },
new Post { PostId = 3, Language = "en", Name = "body", Value = "This is the content for the post 3" },
new Post { PostId = 3, Language = "fr", Name = "title", Value = "Titre de l'article 3" },
new Post { PostId = 3, Language = "fr", Name = "body", Value = "Voici le contenu de l'article 3" },
new Post { PostId = 3, Language = "de", Name = "title", Value = "Titel von Artikel 3" },
new Post { PostId = 3, Language = "de", Name = "body", Value = "Lesen Sie den Inhalt von Artikel 3" }
};
var result = posts
.GroupBy(x => 1)
.ToDictionary(x => "posts",
p => p.GroupBy(bp => bp.PostId)
.ToDictionary(bp => bp.Key,
bp => bp.GroupBy(lp => lp.Language)
.ToDictionary(lp => lp.Key, lp => lp.ToDictionary(n => n.Name, n => n.Value))
)
);
答案2
得分: 0
我认为你需要将这个 DataTable
转换成 JSON。为此,你可以在 .NET 中使用 Newtonsoft Json
库,将数据序列化为有效的 JSON。
类似这样:
string result;
result = JsonConvert.SerializeObject(dt); // 其中 dt 是 DataTable 的内容
更多信息请查阅 Newtonsoft Json 文档。
英文:
I think you need to convert this DataTable
into JSON. For that you use can Newtonsoft Json
Library in .NET and serialize the data to a valid JSON.
Something like this:
string result;
result = JsonConvert.SerializeObject(dt); //where dt is content of DataTable
For more info Newtonsoft Json
答案3
得分: 0
以下是翻译好的部分:
为什么不创建一个代表该对象的类?
这里是一个可以用于您的示例的类:
internal class Post
{
public int PostId { get; set; }
public string Language { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
要将SQL查询结果转换为该类的实例,您可以使用ORM。对于这个示例,您可以考虑使用像Dapper这样的微型ORM,可以查看这个链接:https://www.learndapper.com/dapper-query/selecting-multiple-rows
使用以下代码连接到数据库并执行查询:
using (var connection = new SqlConnection(connectionString))
{
var sql = "SELECT * FROM yourTable"; // 小心:性能!您可能需要添加过滤器
var posts = await connection.QueryAsync<Post>(sql);
foreach (var post in posts)
{
Console.WriteLine($"{post.PostId} {post.Language} {post.Name} {post.Value}");
}
}
以下是使用LINQ执行的一些示例查询:
var engPosts = posts.Where(x => x.Language == "ENG").ToList();
var allTittles = posts.Where(x => x.Name == "Title").ToList();
请记住,根据您的用例,您可能希望在这个基础上创建其他数据结构,以提高性能、可用性、搜索等方面的效果。
英文:
Why not create a class that represent that object?
Here is a class that can be use for your example:
internal class Post
{
public int PostId { get; set; }
public string Language { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
To convert the result of your SQL query into instance of this class, you can use an ORM. For this example you might want to use a micro ORM like dapper who will see this link https://www.learndapper.com/dapper-query/selecting-multiple-rows
using (var connection = new SqlConnection(connectionString))
{
var sql = "SELECT * FROM yourTable"; // Careful: perf! You might want filters
var posts = await connection.QueryAsync<Post>(sql);
foreach(var post in posts)
{
Console.WriteLine($"{post.PostId} {post.Language} {post.Name} {post.Value}");
}
}
Some example query you can do using LINQ:
var engPosts = posts.Where(x=>x.Language == "ENG").ToList();
var allTittles = posts.Where(x=>x.Name=="Title").ToList();
Bear in mind that depending of your use case you might want to have other data structure on top of this one to improve performance, usability, search, etc.
答案4
得分: 0
public class Content
{
public string Title { get; set; }
public string Body { get; set; }
}
public class Post
{
public int Id { get; set; }
public Dictionary<string, Content> LocalizedContent { get; } = new();
}
List<Post> posts = new();
var post = new Post { Id = 1 };
post.LocalizedContent["en"] = new Content {
Title = "Post 1 title",
Body = "This is the content for the post 1"
};
post.LocalizedContent["fr"] = new Content {
Title = "Titre de l'article 1",
Body = "Voici le contenu de l'article 1"
};
posts.Add(post);
// In a DB you would have 2 tables:
TABLE Post
Id int PRIMARY KEY
TABLE Content
PostId int PRIMARY KEY, FOREIGN KEY
Language char(2) PRIMARY KEY
Title varchar(80)
Body varchar(max)
语言也可以作为外键链接到包含受支持语言以及完整语言名称的语言表中。
英文:
You could create an object model consisting of posts containing different language contents.
public class Content
{
public string Title { get; set; }
public string Body { get; set; }
}
public class Post
{
public int Id { get; set; }
public Dictionary<string, Content> LocalizedContent { get; } = new();
}
Example:
List<Post> posts = new();
var post = new Post { Id = 1 };
post.LocalizedContent["en"] = new Content {
Title = "Post 1 title",
Body = "This is the content for the post 1"
};
post.LocalizedContent["fr"] = new Content {
Title = "Titre de l'article 1",
Body = "Voici le contenu de l'article 1"
};
posts.Add(post);
A post contains a dictionary of contents where the language is used as key. This allows you to add different localized contents to a post.
In a DB you would have 2 tables:
TABLE Post
Id int PRIMARY KEY
TABLE Content
PostId int PRIMARY KEY, FOREIGN KEY
Language char(2) PRIMARY KEY
Title varchar(80)
Body varchar(max)
The language could could also be a foreign key into a language table containing supported languages along with the full language name.
答案5
得分: 0
以下是翻译好的部分:
var result = new Dictionary<int, Dictionary<string, PostItem>>();
var postsG = posts.GroupBy(p => new { p.PostId });
foreach (var item in postsG)
{
var posGG = item.GroupBy(i => i.Language);
var dict = new Dictionary<string, Item>();
foreach (var p in posGG)
{
var i = new PostItem();
foreach (var pp in p)
if (pp.Name == "title") i.title = (string)pp.Value;
else i.body = (string)pp.Value;
dict.Add(p.Key, i);
}
result.Add(item.Key.PostId, dict);
}
public class PostItem
{
public string title { get; set; }
public string body { get; set; }
}
输出部分:
JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented);
{
"1": {
"en": {
"title": "Post 1 title",
"body": "This is the content for the post 1"
},
"fr": {
"title": "Titre de l'article 1",
"body": "Voici le contenu de l'article 1"
}
},
"2": {
"en": {
"title": "Post 2 title",
"body": "This is the content for the post 2"
}
},
"3": {
"en": {
"title": "Post 3 title",
"body": "This is the content for the post 3"
},
"fr": {
"title": "Titre de l'article 3",
"body": "Voici le contenu de l'article 3"
},
"de": {
"title": "Titel von Artikel 3",
"body": "Lesen Sie den Inhalt von Artikel 3"
}
}
}
英文:
For the people who prefer c# stricted code
var result = new Dictionary<int, Dictionary<string, PostItem>>();
var postsG = posts.GroupBy(p => new { p.PostId });
foreach (var item in postsG)
{
var posGG = item.GroupBy(i => i.Language);
var dict = new Dictionary<string, Item>();
foreach (var p in posGG)
{
var i = new PostItem();
foreach (var pp in p)
if (pp.Name == "title") i.title = (string)pp.Value;
else i.body = (string)pp.Value;
dict.Add(p.Key, i);
}
result.Add(item.Key.PostId, dict);
}
public class PostItem
{
public string title { get; set; }
public string body { get; set; }
}
output
JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.Indented);
{
"1": {
"en": {
"title": "Post 1 title",
"body": "This is the content for the post 1"
},
"fr": {
"title": "Titre de l'article 1",
"body": "Voici le contenu de l'article 1"
}
},
"2": {
"en": {
"title": "Post 2 title",
"body": "This is the content for the post 2"
}
},
"3": {
"en": {
"title": "Post 3 title",
"body": "This is the content for the post 3"
},
"fr": {
"title": "Titre de l'article 3",
"body": "Voici le contenu de l'article 3"
},
"de": {
"title": "Titel von Artikel 3",
"body": "Lesen Sie den Inhalt von Artikel 3"
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论