【问题标题】:How to return duplicate elements from list c#如何从列表c#中返回重复元素
【发布时间】:2016-02-19 17:50:30
【问题描述】:

如何在“storlek”中返回并显示两个具有相同值的对象?

var item = newklädDataList ."这里有什么东西吗?" (e => e.storlek == searchstring);

我正在制作的应用程序是一个“数字衣橱”,用户可以添加例如带有阿迪达斯、衬衫、黑色、小号元素的衬衫。如果他们搜索“小”,我希望该应用程序显示所有值为“小”的衬衫。 ——

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;            
    }
}

【问题讨论】:

  • 所以你有一个列表,例如不同的衬衫,当用户搜索其中一个值(阿迪达斯、黑色或小号)时,你想返回相关的衬衫吗?跨度>
  • 当你替换 FirstOrDefault 你会得到一个列表。 newklädDataList.Where(e => e.storlek == searchstring).toList();
  • 为什么会有更多列表?我不完全明白你在做什么,这对我来说有点抽象..
  • 但它应该可以用 Where 替换 FirstOrDefault。因为那样你就会得到 storlek == searchstring 的每个列表。
  • 我之前遇到过这个错误,问题是我忘记在方法后面添加“()”。你错过了添加这个的地方吗?

标签: list return duplicates


【解决方案1】:

编辑:

好的,我想我现在明白它应该是什么样子了,如果我错了,请纠正我。 我写了一个小测试程序,我用英语写了所有单词。

所以我有类东西:

    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;
            }
    }

这和你的一样,但我没有把清单放在那里。 然后我有主课:

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);
        }
    }

我有一个列表,我可以在其中添加我想要的所有东西。然后我遍历列表以过滤品牌等于搜索字符串的所有对象(对于您通常不使用 == 的字符串,因为它在那里检查对象引用,这不是您想要的。使用等于它检查正确事物)。我得到了一个 Stuff 对象列表。在 foreach 循环中,我获取列表中的每个项目并将其打印在控制台上。 这就是输出:

brand: nike test3
brand: nike test4

我希望这是你想要的,否则请告诉我。

【讨论】:

  • 我正在制作的应用程序是一个“数字衣橱”,用户可以添加例如带有阿迪达斯、衬衫、黑色、小号元素的衬衫。如果他们搜索“小”,我希望该应用程序显示所有值为“小”的衬衫。
  • 这个答案是您所期望的,还是您需要进一步的帮助?
【解决方案2】:

这是我的原始代码:

 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);
                }

这是我想要的代码:

 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);
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多