LINQ删除重复项和结果项以获得数量总和

时间:2018-05-21 12:44:05

标签: c# linq

我只想检查是否有更快的方法使用LINQ从id重复删除列表,但在结果列表项中将有一些其他属性的总和(在本例中为Price)。例如:

开始列表:

List<Item> a = new List<Item>
{
   new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
   new Item {Id = 2, Name = "Item2", Code = "IT00002", Price = 200},
   new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
   new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
   new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
   new Item {Id = 3, Name = "Item3", Code = "IT00004", Price = 250}
};

结果列表将是:

List<Item> a = new List<Item>
{
  new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 200},
  new Item {Id = 2, Name = "Item2", Code = "IT00002", Price = 200},
  new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 550}
};

4 个答案:

答案 0 :(得分:4)

var filteredList = a.GroupBy(e => e.Id).Select(g =>
{
     var item = g.First();
     return new Item
     {
         Id = item.Id,
         Name = item.Name,
         Code = item.Code,
         Price = g.Sum(e => e.Price)
     };
}).ToList();

答案 1 :(得分:3)

在(功能)LINQ中它类似于:

List<Item> b = a
    .GroupBy(x => x.Id)
    .Select(x => new Item { Id = x.Key, Name = x.First().Name, Code = x.First().Code, Price = x.Sum(y => y.Price) })
    .ToList();

在基于关键字的LINQ中,它类似于:

List<Item> c = (from x in a
                group x by x.Id into y
                select new Item { Id = y.Key, Name = y.First().Name, Code = y.First().Code, Price = y.Sum(z => z.Price) }
               ).ToList();

答案 2 :(得分:1)

这样的事情会做..

var result = a.GroupBy(it => new { it.Id, it.Name, it.Code })
              .Select(x => new { x.Key.Id,x.Key.Name,x.Key.Code,Price = x.Sum(y=>y.Price)});

答案 3 :(得分:-1)

public class Item : IEquatable<Item>
    {
        public int? Id { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        public int? Price { get; set; }

        public bool Equals(Item Other)
        {
            //Check whether the compared object is null. 
            if (Object.ReferenceEquals(Other, null)) return false;

            //Check whether the compared object references the same data. 
            if (Object.ReferenceEquals(this, Other)) return true;

            //Check whether the products' properties are equal. 
            return this.Id == Other.Id;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
    }

List<Item> a = new List<Item>
        {
           new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
           new Item {Id = 2, Name = "Item2", Code = "IT00002", Price = 200},
           new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
           new Item {Id = 1, Name = "Item1", Code = "IT00001", Price = 100},
           new Item {Id = 3, Name = "Item3", Code = "IT00003", Price = 150},
           new Item {Id = 3, Name = "Item3", Code = "IT00004", Price = 250}
        };

        var b = a.Distinct().ToList();