-1

I have code that looks like this:

List<Meme> myList = new List<Meme>();
myList.Add(DogThugLife);
myList.Add(AintNobodyGotTimeForThat);
myList.Add(WashingTheDishes);

I was wondering how I can add a search feature so people can search for the items in this list. I have also made a Meme class:

class Meme
{
    public string Name { get; set; }
    public string Topic { get; set; }
    public bool Popular { get; set; }
    public string Identifier { get; set; }
}

and some info on the items in my list:

Meme DogThugLife = new Meme();
DogThugLife.Name = "Dog Thug Life";
DogThugLife.Topic = "Thug Life";
DogThugLife.Popular = false;

So basically I want users to be able to search for these properties. Thanks!

MethodMan
  • 18,625
  • 6
  • 34
  • 52
NateDev
  • 141
  • 5
  • 14
  • 2
    Check this one http://stackoverflow.com/questions/1175645/find-an-item-in-list-by-linq – starcorn Apr 10 '16 at 21:06
  • It's not clear what you mean by "users search for these properties". Is this a web app, API, or a Winforms app? How will users be interacting with it? (If you just want to know how to search List, @starcorn's link is where to go.) – Nate Barbettini Apr 10 '16 at 21:09
  • http://stackoverflow.com/questions/9854917/how-can-i-find-a-specific-element-in-a-listt – MethodMan Apr 10 '16 at 21:10
  • It's a Winforms app, but I only have the code part not the visual form part yet. Just checked @starcorn's link, thanks for that. – NateDev Apr 10 '16 at 21:12

2 Answers2

0

The List class in C# has a Contains method. That contains method will iterate over the collection and search for an identical object. It does so by invoking the Equals method in System.Object for each object in the collection. Basically, the Contains method receives an object as a parameter. It will pass this object to the System.Equals method for each object in that collection until it finds an identical object or until it has iterated over all of the objects in the collection (didn't find what it was searching for).
This means that you need to override the Equals method in your Meme class, this is an example.

A basic implementation for your Meme class can be like this:

class Meme
{
    public string Name { get; set; }
    public string Topic { get; set; }
    public bool Popular { get; set; }
    public string Identifier { get; set; }

    public bool Equals(object other)
    {
        var casted = other as Meme;
        if(null == casted)
        {
             return false;
        }

        return casted.Name == Name && Casted.Topic == Topic and Casted.Popular == Popular && Casted.Identifier == Identifier;
    }

    //IMPORTANT: The Equals and the GetHashCode methods are intertwined. Thus, when you override one of them, you should override the other.
    //The rule is: The GetHashCode method should return the same value for two objects that are equals (according to their Equals method)
    //More about the GetHashCode method: https://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx
    public int GetHashCode()
    {
        //Naive implementation
        return Name.GetHashCode();
    }
}

And then:

var myList = new List<Meme>();
// add items to list.
var dogThugLife = new Meme();
// initialize dogThugLife object's properties (though you should use a constructor for that)
var result = myList.Contains(dogTugLife);

It is important to note that there are better ways in terms of simplicity and performance to search for objects in a collection. If you know from advance that you will need to search for an object and you know that you won't have only a few number of items in that collection, then you should consider using a Dictionary or a HashSet instead of a list.

A simple example for using a HashSet:

var mySet = new HashSet<Meme>();
// add items to set (example: mySet.Add(someMeme)).
var dogThugLife = new Meme();
// initialize dogThugLife object's properties (though you should use a constructor for that)
var result = mySet.Contains(dogTugLife); // This will search the set via the GetHashCode value of dogTugLife
m1o2
  • 1,549
  • 2
  • 17
  • 27
0

A sample program showing how to peek a meme object by its identifier from a generic list

using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;

namespace SOFAcrobatics
{
    class Meme
    {
        public String Name
        {
            get;
            set;
        }

        public String Topic
        {
            get;
            set;
        }

        public Boolean Popular
        {
            get;
            set;
        }

        public String Identifier
        {
            get;
            set;
        }

        public Meme (String name, String topic, Boolean popular, String identifier)
        {
            this.Name = name;
            this.Topic = topic;
            this.Popular = popular;
            this.Identifier = identifier;
        }

        public override string ToString()
        {
            return String.Format("(Identifier: {0}, Topic: {1}, Popular: {2}, Name: {3})",
                this.Identifier,
                this.Topic,
                this.Popular,
                this.Name);
        }
    }

    public static class Launcher
    {
        public static void Main ()
        {
            List<Meme> memes = new List<Meme>() {
                new Meme ("name1", "topic1", false, "id1"),
                new Meme ("name3", "topic2", true, "id2"),
                new Meme ("name2", "topic3", false, "id3")
            };

            MessageBox.Show(Launcher.SearchByIdentifier(memes, "id2").ToString(),
                "Search result for Meme name3",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        private static Meme SearchByIdentifier (List<Meme> memes, String identifier)
        {
            return memes.Find(
                m => (m.Identifier == identifier)
                );
        }
    }
}
marsouf
  • 1,107
  • 8
  • 15