How to return duplicate elements from list c#

How can i return and displays two objects with the same value in "storlek" ?

var item = newklädDataList ."maybe something here?" (e => e.storlek == searchstring);

The app i'm making is a "digital wardrobe" and users can add for exampel a shirt with the elements adidas, shirt, black, small. And i want the app to show all shirts with the value "small" if they search for "small". –

string searchstring = Console.ReadLine().ToUpper();


        var item = newklädDataList.FirstOrDefault(e => e.storlek == searchstring);

        if (item != null)
        {
            Console.Clear();
            menuTEXT.WriteFullLine("- Din Digitalgarderobs innehåll -");
            Console.WriteLine("~========================================================================~");
            Console.WriteLine("                                                                        ");
            Console.WriteLine("Märke: {0}\tTyp: {1}\tFärg: {2}\tStorlek: {3}                          ", item.märke, item.typ, item.färg, item.storlek);
            Console.WriteLine("                                                                        ");
            Console.WriteLine("~========================================================================~");
            Felhantering.sökaIgen();
        }
        else if (item == null)
        {
            nullResult(searchstring);
        }

public class klädDATALIST //Kläddata sparas här
{

    private static List<klädDATALIST> newklädDataList = new List<klädDATALIST>(); // Lista med klädegenskaper

    public static List<klädDATALIST> GetList() // Metod för att hämta lista
    {
        return newklädDataList;
    }

    public string märke;
    public string typ;
    public string färg; 
    public string storlek;        

    public klädDATALIST(string _märke, string _typ, string _färg, string _storlek) //Överlagrad konstruktor
    {
        this.märke = _märke;
        this.typ = _typ;
        this.färg = _färg;
        this.storlek = _storlek;            
    }
}

Upvotes: 0

Views: 64

Answers (2)

This was my original code:

 if (newklädDataList[i].färg.Contains(item.färg))
                {                        
                    Console.WriteLine("Märke: {0}\tTyp: {1}\tFärg: {2}\tStorlek: {3}                         ", item.märke, item.typ, item.färg, item.storlek);
                }

And this is my code that works as i wanted it:

 if (newklädDataList[i].färg.Contains(item.färg))
                {
                    klädDATALIST plagg = newklädDataList[i];
                    Console.WriteLine("Märke: {0}\tTyp: {1}\tFärg: {2}\tStorlek: {3}                         ", plagg.märke, plagg.typ, plagg.färg, plagg.storlek);
                }

Upvotes: 0

Florin M
Florin M

Reputation: 445

EDIT:

Ok, I think I understand now how it should look like, correct me when im wrong. I wrote a litle test programm where i wrote all the words in english.

so i have the class Stuff:

    public class Stuff{

            public string brand;
            public string type;
            public string color;
            public string size;

            public Stuff(string argBrand, string argType, string argColor, string argSize){
                brand = argBrand;
                type = argType;
                color = argColor;
                size = argSize;
            }
    }

this is the same as yours, but I didn't put the list in there. Then i have the main class:

public class Test
{

    public static void Main()
    {
        List<Stuff> AllStuff = new List<Stuff>();

        Stuff stuff = new Stuff("adidas", "test", "test", "test");
        Stuff stuff2 = new Stuff("adidas", "test2", "test", "test");
        Stuff stuff3 = new Stuff("nike", "test3", "test", "test");
        Stuff stuff4 = new Stuff("nike", "test4", "test", "test");
        Stuff stuff5 = new Stuff("puma", "test5", "test", "test");
        AllStuff.Add(stuff);
        AllStuff.Add(stuff2);
        AllStuff.Add(stuff3);
        AllStuff.Add(stuff4);
        AllStuff.Add(stuff5);

        string searchstring = "nike";

        var items = AllStuff.Where(s => s.brand.Equals(searchstring)).ToList();

        foreach(var item in items){
            Console.WriteLine("brand: " + item.brand + " " + item.type);
        }
    }

There I have a list where I add all the Stuff I want. Then I iterate over the list to filter all objects where the brand is equals the searchstring (for strings you normaly not take the == because there it checks the object reference, and this is not what you want to. with equals it checks the correct thing). There i got a list of Stuff objects back. In the foreach loop I take every item in the list and print it on the console. And thats the output:

brand: nike test3
brand: nike test4

I hope this is what you wanted, otherwise let me know.

Upvotes: 1

Related Questions