Remove duplicates in the list using linq

前端 未结 11 1703
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 03:04

I have a class Items with properties (Id, Name, Code, Price).

The List of Items is populated with duplicated items.

F

11条回答
  •  梦谈多话
    2020-11-22 03:33

    Another workaround, not beautiful buy workable.

    I have an XML file with an element called "MEMDES" with two attribute as "GRADE" and "SPD" to record the RAM module information. There are lot of dupelicate items in SPD.

    So here is the code I use to remove the dupelicated items:

            IEnumerable MList =
                from RAMList in PREF.Descendants("MEMDES")
                where (string)RAMList.Attribute("GRADE") == "DDR4"
                select RAMList;
    
            List sellist = new List();
    
            foreach (var MEMList in MList)
            {
                sellist.Add((string)MEMList.Attribute("SPD").Value);
            }
    
            foreach (string slist in sellist.Distinct())
            {
                comboBox1.Items.Add(slist);
            }
    

提交回复
热议问题