user3474335
user3474335

Reputation: 1

Comparing IEnumerable string to SortedList string,string

I ran into a problem I cannot solve. :-) I have to find all the values which contains a certain substring then I must get back the key and value pair. I had to implement a system where I had to make a SortedList, where the Albums is a class string is the key of course

Albums alb = new Albums();
SortedList<string, Albums> list1 = new SortedList<string, Albums>();

The Albums class looks like this:

public class Albums : IComparable
{
    public string albname;
    public string name1;
    public string releasedate;
    public Guid albumID;

   public Albums()
    {
        albumID = new Guid();
    }



   public override string ToString()
   {
       return "Album: " + Albname + "\t" + Releasedate;
   }

   public Albums(string albname, string releasedate)
   {

       this.albname = albname;
       this.releasedate = releasedate;
   }

   public string Name1
   {
       get { return name1; }
       set { name1 = value; }
   }

    public string Albname
    {
        get { return albname; }
        set { albname = value; }
    }
   public string Releasedate
    {
        get { return releasedate; }
        set { releasedate = value; }
    }
    public int CompareTo(object obj)
    {
        if (obj is Albums)
        {
            Albums other = (Albums)obj;
            return albname.CompareTo(other.albname);
        }
       if (obj is string)
       {
           string other = (string)obj;
           return releasedate.CompareTo(releasedate);
       }


        else
        {
            return -999;
        }
    }
   }

What I tried at last that I put the Albums into a LinkedList:

LinkedList<string> albm1 = new LinkedList<string>();  

I did manage to find all the Albums that contains the substring using IEnumerable:

string albsearch = textBox16.Text;
IEnumerable<string> result = albm1.Where(s => s.Contains(albsearch));

BUT I do not know how to compare result to the Values of the SortedList. I also tried to create a new SortedList which contains the album in string:

SortedList<string, string> list2 = new SortedList<string, string>();

Any suggestion would be appreciated. Thanks in advance!

Upvotes: 0

Views: 466

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 134045

When you enumerate a SortedList, each item in the enumeration is a key/value pair.

I think what you want is:

Albums alb = new Albums();
SortedList<string, Albums> list1 = new SortedList<string, Albums>();

var foundItems = list1.Where(item => item.Key.Contains(albsearch));

Or, if you want to search in the Album:

var foundItems = list1.Where(item => item.Value.albname.Contains(albsearch));

Of course, you could search the name1 field rather than the album name, if that's what you want.

Each item returned is a key/value pair. More correctly, a KeyValuePair<string, Album>.

Upvotes: 1

Michal Ciechan
Michal Ciechan

Reputation: 13898

    [Test]
    public void Allbum_Search_Test()
    {
        var searchText = "TestA";
        var list1 = new SortedList<string, Albums>
        {
            {"TestAlbum1", new Albums("TestAlbum1","SomeDate")},
            {"TestAlbum2", new Albums("TestAlbum2","SomeDate")},
            {"OtherAlbum2", new Albums("OtherAlbum","SomeDate")}
        };

        var results = list1.Where(pair => pair.Key.Contains(searchText));

        Assert.That(results.Count(), Is.EqualTo(2));
    }

On another note i would highly recommend

  • making the fields private
  • renaming Albums to Album
  • changing the ReleaseDate type to DateTime
  • renaming Albname to AlbumName

Upvotes: 0

Related Questions