从自定义列表对象中删除重复项

时间:2019-10-03 13:11:51

标签: c# asp.net asp.net-mvc

摘要:从自定义列表列表中删除重复项

我尝试使用各种现有方法从其他堆栈溢出帖子中删除重复项,例如:

var distinctWords = combinedList.Distinct().ToList();

fooArray.GroupBy(x => x.Id).Select(x => x.First());

这些解决方案仅根据1个条件过滤,我需要根据2个条件删除重复项,如果年份和ID匹配,则需要删除重复项。可能有不同的年份具有相同的ID。我想不使用MoreLINQ来做到这一点。

型号:

    public class data
    {
        public List<Inventory> inventory { get; set; }
        public List<LocationInventory> locationinventory { get; set; }

    }

    public class Inventory
    {
        public string name{ get; set; }
        public string year{ get; set; }
        public string id{ get; set; }
    }

    public class LocationInventory
    {
        public string location { get; set; }
        public List<Inventory> inventory { get; set; }
    }

控制器:

        public ActionResult getData(string[] Make, string[] Location)
        {
            data inv = new Models.data();

            //Make a list of the inventory lists
            inv.locationinventory = new List<LocationInventory>();

            //Split the Manufacturer and Location at the commas and store in new array
            string[] Make_Values = Make[0].Split(',');
            string[] Location_Values = Location[0].Split(',');

            //Get values for the tables
            var popData = new List<Inventory>();

            foreach (var Location_Value in Location_Values)
            {
                //Populate the data for each location
                //populateData returns a List<Inventory>
                popData = populateData(Make_Values, Location_Value.ToString());

                //Add the data to model with cooresponding location and inventory
                inv.locationinventory.Add(new LocationInventory{ location = Location_Value, inventory = popData });
            }

            return PartialView("Table", inv);
        }

查看:

@{
    var inventories = Model.locationinventory.Select(x => x.inventory).Distinct().ToList();
    var list1 = new List<Project1.Models.Inventory>();
    var list2 = new List<Project1.Models.Inventory>();

    foreach (var inv in inventories)
    {
        if (list1?.Any() != true)
        {
            list1 = inv;
        }

        if (list1?.Any() != false)
        {
            list2 = inv;
        }

        var combinedList = list1.Union(list2);

        var test = combinedList.GroupBy(x => new { x.id, x.year }).ToList();

        <div>
            <table>                
                @foreach (var l in test)
                {
                    foreach (var m in l)
                    {
                        <thead>
                            <tr>
                                <td>@m.name</td>
                                <td>@m.year</td>
                                <td>@m.id</td>
                            </tr>
                        </thead>
                    }
                }
            </table>
        </div>
    }

}

当前结果:

iphone 2019 x1234
iphone 2019 x1234
iphone 2020 x1234
iphone 2018 x1234
iwatch 2019 a4321
iwatch 2020 a4321
android 2020 n9876
android 2020 n9876
android 2019 n9876

预期结果:

iphone 2019 x1234
iphone 2020 x1234
iphone 2018 x1234
iwatch 2019 a4321
iwatch 2020 a4321
android 2020 n9876
android 2019 n9876

1 个答案:

答案 0 :(得分:3)

您在某种程度上接近您的需求,但还不完整。分组后,您只需要从每个组中投射一行,例如:

var uniqueInventory = combinedList.GroupBy(x => new { x.name,x.id, x.year })
                                  .Select(x => x.First())
                                  .ToList();

现在,每个组将包含一个项目,并且所有项目都是唯一的条目。

另一种方法是实现IEqualityComparer,该方法可以通过Distinct方法传递。

public class InventoryComparer: IEqualityComparer<Inventory>
{
    public override bool Equals(Inventory a, Inventory b)
    {
        return a.name == b.name
            && a.id == b.id
            && a.year == b.year;
    }

    public override int GetHashCode(Inventory inventory)
    {
        return inventory.id.GetHashCode();
    }
}

现在我们可以像这样使用它了:

var uniqueInventory = combinedList.Distinct(new InventoryComparer());
相关问题