rj487
rj487

Reputation: 4634

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>>. Every article has its own titleNum which is used to record article number.

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 inputTitleNumto the getComment function, it should return a commentList.

Upvotes: 0

Views: 795

Answers (4)

Fabjan
Fabjan

Reputation: 13676

If i understand your question correctly for this purpose you can use 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. 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.

Let's say you have Comment entity in your database like this:

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 :

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);
    }

Upvotes: 3

Cardin
Cardin

Reputation: 5507

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.

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;
    }
}

Upvotes: 2

MIKE
MIKE

Reputation: 1059

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.

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; set; } public string Creator { get; set; } public DateTime DateCreated { get; set; } public List Comments { 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; }
}

Upvotes: 0

Sam Axe
Sam Axe

Reputation: 33738

First, what you have written is not valid C#. It should be something like:
private KeyValuePair<int, List<string>> Data {get; set; }

then your addComment function would look like:

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:

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

As per the KeyValuePair contract, you would retrieve it via

Upvotes: 0

Related Questions