如何从列表中获取最后一个重复项

时间:2015-09-30 05:48:37

标签: c# .net windows linq

我有一个包含重复项目的列表,我需要的是只获取该项目的最后一项。

我尝试做的事情如下,但无法找到任何进一步的解决方案来实现我的目标。

     List<ListItem> listItems = new List<ListItem>
        {
            new ListItem{ Item1="Item1", Item2="Item2"},
            new ListItem{ Item1="Item2", Item2="Item22"},
            new ListItem{ Item1="Item3", Item2="Item23"},
            new ListItem{ Item1="Item4", Item2="Item24"},
            new ListItem{ Item1="Item4", Item2="Item244"},
            new ListItem{ Item1="Item4", Item2="Item244"},
            new ListItem{ Item1="Item5", Item2="Item25"},
            new ListItem{ Item1="Item1", Item2="Item12"},
            new ListItem{ Item1="Item6", Item2="Item26"},
            new ListItem{ Item1="Item7", Item2="Item27"},
            new ListItem{ Item1="Item1", Item2="Item22"},
            new ListItem{ Item1="Item8", Item2="Item28"},
            new ListItem{ Item1="Item9", Item2="Item29"},
            new ListItem{ Item1="Item8", Item2="Item288"},
            new ListItem{ Item1="Item10", Item2="Item210"},
            new ListItem{ Item1="Item8", Item2="Item288"},
            new ListItem{ Item1="Item11", Item2="Item211"},
        };

        listItems.GroupBy(x => x.Item1).ToList().ForEach(r =>
                {
                    //Want to get last item w.r.t. "r". because I have a complex scenario that if something does not works I also have to get second  last record, so I don't want to lose GroupBy list.
                    foreach (var item in r)
                 {
                        Console.WriteLine(item.Item1 + " " + item.Item2);
                 }
                });

预期产出:

Item1 Item22
Item2 Item22
Item3 Item23
Item4 Item244
Item5 Item25
Item6 Item26
Item7 Item27
Item8 Item288
Item9 Item29
Item10 Item210
Item11 Item211

3 个答案:

答案 0 :(得分:2)

假设副本基于Item,那么只需获取Last中的GroupBy元素。

var nodups = listItems
   .GroupBy(x => x.Item1)
   .Select(g => g.Last());
foreach (var item in nodups)
   Console.WriteLine(item.Item1 + " " + item.Item2);

答案 1 :(得分:1)

我猜你需要每个Item1最后一项: -

var result = listItems.GroupBy(x => x.Item1)
                      .Select(x => new
                            {
                               Item1 = x.Key,
                               Item2 = x.OrderByDescending(z => z.Item2).First().Item2
                            });

Working Fiddle.

<强>更新

我猜你需要List<ListItem>作为输出类型。好吧,你可以使用它: -

List<ListItem> res = listItems.GroupBy(x => x.Item1)
                              .Select(x =>
                                  {
                                       var lastItem = x.Last(); 
                                            //Or x.OrderByDescending(z => z.Item2).First()
                                       return new ListItem
                                          {
                                             Item1 = x.Key,
                                             Item2 = lastItem.Item2
                                          };
                                  }).ToList();

获得以下输出: -

enter image description here

答案 2 :(得分:1)

小提琴:https://dotnetfiddle.net/TMAcie

    var result = listItems
                .GroupBy(x=>new {x.Item1,x.Item2})
                .Select(g=>new {Item1 = g.Key.Item1,Item2 = g.Max(m=>m.Item2)});

如果重复条件的标准是Item1

    var result = listItems
                .GroupBy(x=>x.Item1)
                .Select(g=>new {Item1 = g.Key,Item2 = g.Max(m=>m.Item2)});