0

I have a HashSet filled with about 50 posts which I want to pair in two by two into my database (the posts are a title and a description that belong together). The problem is that I cant get the logic together. This code below maybe explains a little better what I am thinking of:

foreach(string item in hash)
{
    // Here something that assigns every uneven HashSet-post to item1, the even ones to item2
    var NewsItem = new News 
    {
        NewsTitle = item1
        NewsDescription = item2
    };


    dbContext db = new dbContext();

    db.News.Add(NewsItem);
    db.SaveChanges();
}
Colin Basnett
  • 4,052
  • 2
  • 30
  • 49
user3581054
  • 125
  • 12
  • 4
    How do you define "even" and "odd" items? A HashSet doesn't have an "order" so it doesn't seem like the right data structure for your needs. – D Stanley May 20 '14 at 18:38
  • I define them as the first item in the HashSet is odd(1), and the second even(2), etc. I figured this would be the best way to loop through them since they are in pairs. In order to have the right values inserted to the right columns in the database. – user3581054 May 20 '14 at 18:46
  • 1
    @user3581054, see Remarks on the [HashSet](http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx) for the reference. What D Stanley is saying is that the order of the results you will receive is not guaranteed. So you can expect title1, descr1, title2, descr2, and yet receive title1, title2, descr1, descr2 or any other permutation. – Andrei May 20 '14 at 18:50

2 Answers2

3

You cannot "pair up" items from hash-based containers, because from the logical standpoint these containers are ordered arbitrarily *.

Therefore, you need to pair up the titles and descriptions when you insert your data into hash sets, like this:

class Message {
    public string Title {get;set;}
    public string Description {get;set;}
    public int GetHashCode() {return 31*Title.GetHashCode()+Description.GetHashCode();}
    public bool Equals(object other) {
        if (other == this) return true;
        Message obj = other as Message;
        if (obj == null) return false;
        return Title.Equals(obj.Title) && Description.Equals(obj.Description);
    }
}
ISet<Message> hash = new HashSet<Message>();

At this point you can insert messages into your hash set. The titles and descriptions will be always paired up explicitly by participating in a single Message object.

* The current implementation from Microsoft does maintain the insertion order, but this is an unfortunate implementation detail.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I will look in to that! Do you have any useful articles for me to study in order to somewhat understand that code alittle better? Im quite new to programming. – user3581054 May 20 '14 at 18:55
  • @user3581054 I assume that you understood the two lines that declare the properties `Title` and `Description`, right? Then the only non-trivial issue is the hash code and equality computation. You can read more about it in [this Microsoft article](http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx), and in [this answer](http://stackoverflow.com/q/371328/335858). – Sergey Kalinichenko May 20 '14 at 19:01
  • Yes that I did :) Thank you – user3581054 May 20 '14 at 19:03
-1

I define the first item in the HashSet is odd(1), and the second even(2), etc.

Then a HashSet is not the right data structure. HastSets are not in any particular order, so if you need to extract the items sequentially then a plain List<string> would work.

That said, one way to do what you need is to use a for loop that gets items two-at-a- time:

using(dbContext db = new dbContext())
{
    for(int i = 0; i < list.Count - 1; i += 2)
    {
        var NewsItem = new News 
        {
            NewsTitle = list[i];
            NewsDescription = list[i+1];
        };

        db.News.Add(NewsItem);
    }
}
db.SaveChanges();
D Stanley
  • 149,601
  • 11
  • 178
  • 240