简体   繁体   English

定义一个KeyValuePair结构,例如 <int,List<string> &gt;

[英]Define a KeyValuePair struct like <int,List<string>>

I was working on a online forum project, and I want to add some comment on the article. 我正在从事一个在线论坛项目,我想对这篇文章添加一些评论。

In my imagination, I may need to create some structure like KeyValuePair <int,List<string>> . 在我的想象中,我可能需要创建一些结构,例如KeyValuePair <int,List<string>> Every article has its own titleNum which is used to record article number. titleNum文章都有自己的titleNum ,用于记录文章编号。

In my program, it should be like 在我的程序中,它应该像

public void addcomment(int titleNum, string comment) 
{
    int title = 
    using Data = System.Collections.Generic.KeyValuePair<int, List<string>>
    List<string> a = new List<string>;
    a.add(comment)
    //then append List a to Data
}

public List<string> getComment (int inputTitleNum)
{
 //return string List which titleNum = inputTitleNum
}

However, I don't know what to do now. 但是,我不知道该怎么办。 In final result, it should be like this. 最终结果应该是这样。 When I input inputTitleNum to the getComment function, it should return a commentList . 当我向getComment函数输入inputTitleNum ,它应该返回commentList

If i understand your question correctly for this purpose you can use Dictionary<int, List<string>> 如果我为此目的正确理解了您的问题,则可以使用Dictionary<int, List<string>>

    Dictionary<int, List<string>> articles = new Dictionary<int, List<string>>();

    public void AddComment(int titleNum, string comment) 
    {
        if(!articles.Keys.Contains(titleNum)) articles.Add(titleNum, new List<string>());
        articles[titleNum].Add(comment);
    }

    public List<string> getComment (int titleNum)
    {
       return articles[titleNum];
    }

But you should really consider using Entity Framework, SQL database and Repository. 但是您应该真正考虑使用实体框架,SQL数据库和存储库。 After that you can use Comments and Articles models to work with data from your database. 之后,您可以使用评论和文章模型来处理数据库中的数据。 You can even create and use viewmodels for example CommentList to put your data to view. 您甚至可以创建和使用诸如CommentList之类的视图模型来查看数据。

Let's say you have Comment entity in your database like this: 假设您在数据库中具有这样的Comment实体:

public class Comment
{
    public long Id { get; set; }
    public string Title{ get; set; }
    public string Text { get; set; }
    public long ArticleId { get; set; }
}

And entity Article something like this: 和实体文章是这样的:

public class Article
{
    public long Id{ get; set; }
    public string Header { get; set; }
    public string Text { get; set; }        
    public long AuthorId { get; set; }
}

Now you can add methods to your Repository class that work with that models : 现在,您可以向与该模型一起使用的方法添加到Repository类中:

public class Repository : IRepository
{
   .... 
   private MyDbContext _cont;

   public Comment getComment (int Id)
   {
       return _cont.Comments.FirstOrDefault(c => c.Id == Id);
   }

   public List<Comments> GetCommentsList(int articleId)
   {
       return _cont.Comments.Where(c => c.ArticleId == aricleId).ToList();
   }
}

And in your controller use it like this: 并在您的控制器中使用它,如下所示:

    public ActionResult GetComments(long Id)
    {
        object model = List<Comment> comments = Repo.GetCommentsList(Id);
        return View(model);
    }

About your method itself, you should use a database if you have a lot of comments or you want to form associations. 关于方法本身,如果您有很多注释或想要形成关联,则应该使用数据库。 Etc. associate with date, user. 与日期,用户等相关联。 It'd be faster for retrieval etc. Consider looking up SQLite, which is a database stored as a text file. 它可以更快地进行检索等。考虑查找SQLite,这是一个存储为文本文件的数据库。

If you're just satisfied with using a list though, this should work: 如果您只是满意使用列表,则应该可以使用:

public class Article
{
    public Dictionary<int, List<string>> articleComments = new Dictionary<int, List<string>>();

    public void AddComment(int titleNum, string comment)
    {
        if (!articleComments.ContainsKey(titleNum))
        {
            articleComments.Add(titleNum, new List<string>());
        }
        articleComments[titleNum].Add(comment);
    }

    public List<string> GetComment(int titleNum)
    {
        if (articleComments.ContainsKey(titleNum)
        {
            return articleComments[titleNum];
        }
        return null;
    }
}

First, what you have written is not valid C#. 首先,您编写的内容无效的C#。 It should be something like: 应该是这样的:
private KeyValuePair<int, List<string>> Data {get; set; }

then your addComment function would look like: 那么您的addComment函数将如下所示:

public void addComment(int titleNum, string comment) {
    if (!this.Data.ContainsKey(titleNum) {
        this.Data.Add(titleNum, new List<string>());
    }
    this.Data[titleNum].Add(comment);
}

and getComment: 和getComment:

public List<string> getComment(int titleNum) {
    return this.Data[titleNum];
}

As per the KeyValuePair contract, you would retrieve it via 根据KeyValuePair合同,您可以通过

Using a dictionary to do this would not be the best approach especially when you decide to use a database. 使用字典来做到这一点并不是最好的方法,尤其是当您决定使用数据库时。 You should be storing a list of comments inside your Article class. 您应该在Article类中存储评论列表。

 public class Article { public int ArticleNum { get; set; } public string Creator { get; set; } public DateTime DateCreated { get; set; } public List<string> Comments { get; set; } 
    public Article()
    {
        this.Comments = new List<string>();
    }

    public void AddComment(string comment)
    {
        this.Comments.Add(comment);
    }

    public void RemoveComment(string comment)
    {
        this.Comments.Remove(comment);
    }
}

You could also get rid of the string list for comments and create a comment class and have a comment list instead. 您也可以删除注释的字符串列表,然后创建一个注释类,并添加一个注释列表。

public class Article { public int ArticleNum { get; 公共类文章{public int ArticleNum {get; set; 组; } public string Creator { get; } public string Creator {取得; set; 组; } public DateTime DateCreated { get; } public DateTime DateCreated {get; set; 组; } public List Comments { get; }公开列表评论{get; set; 组; } }

    public Article()
    {
        this.Comments = new List<Comment>();
    }

    public void AddComment(Comment comment)
    {

        this.Comments.Add(comment);
    }

    public void RemoveComment(Comment comment)
    {
        this.Comments.Remove(comment);
    }
}
public class Comment
{
    public string Comment { get; set; }
    public string Creator { get; set; }
    public DateTime DateCreated { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定义一个像 List 这样的 List<int,string> ? - define a List like List<int,string>? 将string []转换为List <KeyValuePair<int, string> &gt; - Turn string[] into List<KeyValuePair<int, string>> 排序列表 <KeyValuePair<int, string> &gt;大写和小写 - Sorting a List<KeyValuePair<int, string>> Uppercase and Lowercase gridview绑定下拉列表到列表 <keyvaluePair<int, string> &gt; - gridview bind dropdownlist to List<keyvaluePair<int, string>> c#排序列表 <KeyValuePair<int, string> &gt; - c# Sorting a List<KeyValuePair<int, string>> 如何将int数组转换为List <KeyValuePair<int, string> &gt;? - How to convert int array to List<KeyValuePair<int, string>>? 如何正确使用列表上的LINQ查询 <KeyValuePair<string, string> 创建字典 <KeyValuePair<string, string> ,int&gt;? - How to properly use LINQ Query on List<KeyValuePair<string, string> to create Dictionary<KeyValuePair<string, string>, int>? 如何访问列表 <KeyValuePair<string,int> &gt;来自MongoDB - How can I access List<KeyValuePair<string,int>> from MongoDB 保存清单 <KeyValuePair<int,string> 到使用EF 4.3的数据库 - Saving List<KeyValuePair<int,string> to a DB with EF 4.3 映射列表<KeyValuePair<string,int> &gt; 在 C# 中自定义对象 - Mapping List<KeyValuePair<string,int>> to custom object in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM