在<T> C# 中通过 foreach 循环消除列表中的重复项



如何从项目列表中消除重复的项目(时间戳)。我有一个具有重复时间戳的项目列表。我想填补另一个响应列表,并消除重复记录的基础上的唯一时间戳为每个循环。返回项列表中只能出现一个时间戳。

    public class InventoryDetails
    {
        public int InventoryDetailsId { get; set; }
        public int ItemName { get; set; }
        public int Price { get; set; }
        public DateTime Timestamp { get; set; }
    }
    public class InventoryDetailsResponse
    {
        public int InventoryDetailsId { get; set; }
        public int ItemName { get; set; }
        public int Price { get; set; }
        public DateTime Timestamp { get; set; }
    }

数据库中的样本库存数据

101年Item1, 500, 2015-06-24 16:00:03
102、Item2, 125, 2015-07-01 01:20:03
103、Item1, 500, 2015-06-24 16:00:03
104、item3,340, 2015-07-04 09:10:12
105、Item4, 059, 2015-06-24 12:23:03
106、Item1, 500, 2015-06-24 16:00:03
107、Item5、845、2015-07-11 15:30:03

//需要根据时间戳删除重复的记录

public List<InventoryDetailsResponse> GetInventory()
        {
            List<InventoryDetails> result = FromDatabase();
            var list = new List<InventoryDetailsResponse>();
            foreach (InventoryDetails match in result)
            {
                var tc = new InventoryDetailsResponse
                {
                    InventoryDetailsId = match.InventoryDetailsId,
                    ItemName = match.ItemName,
                    Price = match.Price,
                    Timestamp = match.Timestamp  // Duplicate timestamp in database.
                };
                list.Add(tc);
            }
            return list;
        }

直接使用Linq:

var myUniqueRecordsByTimestamp = GetInventory()
                                .GroupBy(x => x.Timestamp)
                                .Select(g => g.First());

GroupBy操作将返回IGrouping项(也是IEnumerables项)的IEnumerable

类似但不重复问题的源答案

我也用过这个,效果很好:

List<InventoryDetails> list = result.GroupBy(x => x.Timestamp).Select(y => y.First()).ToList();

它根据指定的属性(这里是Timestamp)对条目进行分组,并使用每组的第一项构建一个新列表。

您可以使用LINQ:

public List<InventoryDetailsResponse> GetInventory()
{
    List<InventoryDetails> result = FromDatabase();
    var list = new List<InventoryDetailsResponse>();
    foreach (InventoryDetails match in result)
    {
        var tc = new InventoryDetailsResponse
        {
            InventoryDetailsId = match.InventoryDetailsId,
            ItemName = match.ItemName,
            Price = match.Price,
            Timestamp = match.Timestamp  // Duplicate timestamp in database.
        };
        list.Add(tc);
     }
    var res = list.GroupBy(item => item.Timestamp).Select(g => g.First()).ToList();
    return res;
}

您可以为您的InventoryDetails类重写Equals,如:

class InventoryDetails {
    public override Equals(object obj) {
        if (obj is InventoryDetails) {
            InventoryDetails details = (InventoryDetails)obj;
            return details.ItemName == this.ItemName && details.Price == this.Price && details.Timestamp == this.Timestamp;
        }
        return false;
    }
}

之后,您可以使用List<InventoryDetails>.Distinct()来删除重复的条目,因为所有具有相同Timestamp的条目现在都被认为是相等的。

最新更新