0

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!

4
  • 1
    So you want to find all the albums that contain a certain string - and then sort them? What is the key?
    – JEV
    Mar 31, 2014 at 15:33
  • Thank you for your reply! The key is a name a string Mar 31, 2014 at 15:36
  • string name = textBox1.Text; Mar 31, 2014 at 15:37
  • Basically the name of the artist or band. Because I need to find the Artist or Band using substring through the Albums Mar 31, 2014 at 15:39

2 Answers 2

1

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>.

0
0
    [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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.