LINQ用于获取出现次数大于1的列表

时间:2019-01-30 08:30:32

标签: c# .net linq

下面的类中有一个列表

public class DummyClass
{
    public string name { get; set; }
    public int value { get; set; }
    public string status { get; set; }
}

原始列表和预期输出如下所示

enter image description here

它应该满足以下条件:

如果存在状态= A的名称(例如A1和A2)->删除包含相同名称和状态= I的其他同级项(假定每个名称仅一个状态= A) 如果一个名称包含很多status = I,但不包含status = A的项目,则仅显示1个具有该名称的项目

它可以通过很少的for循环来实现,并从字面上删除重复的对等点,问题在于它是否可以通过LINQ来实现

2 个答案:

答案 0 :(得分:5)

如何?

var list = new[]
{
    new DummyClass{ Name = "A1", Value=1, Status= "A" },
    new DummyClass{ Name = "A1", Value=2, Status= "I" },
    new DummyClass{ Name = "A2", Value=3, Status= "I" },
    new DummyClass{ Name = "A2", Value=4, Status= "I" },
    new DummyClass{ Name = "A2", Value=5, Status= "A" },
    new DummyClass{ Name = "A3", Value=6, Status= "I" },
    new DummyClass{ Name = "A3", Value=7, Status= "I" },
};

var aggregate = list
    .OrderBy(item => item.Status)
    .GroupBy(item => item.Name)
    .Select(group => group.First());

foreach (var item in aggregate)
{
    Console.WriteLine($"{item.Name} - {item.Value} - {item.Status}");
}

它将以状态为第一顺序(“ A”在“ I”之前),然后按给定名称分组。因此,每个组的第一个元素将是第一个“ A”条目,或者丢失第一个“ I”条目。

输出:

  

A1-1-A

     

A2-5-A

     

A3-6-I

答案 1 :(得分:3)

这是您获得预期收藏的LINQ:

collection.GroupBy(g => g.name)
          .ToDictionary(d => d.Key, d => (d.OrderBy(or => or.status).First()));

collection是表中所有行的集合。

这是检查它的代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestConsoleApp
{
    class DummyClass
    {
        public string name { get; set; }
        public int value { get; set; }
        public string status { get; set; }
    }

    class DummyClassCollection
    {
        private List<DummyClass> _collection;

        public DummyClassCollection()
        {
            _collection = new List<DummyClass>();
            FillUpCollection();
        }

        public void FillUpCollection()
        {
            _collection.Add(new DummyClass(){
                name = "A1",
                value = 1,
                status = "A"
            });
            _collection.Add(new DummyClass(){
                name = "A1",
                value = 2,
                status = "I"
            });
            _collection.Add(new DummyClass(){
                name = "A2",
                value = 3,
                status = "I"
            });
            _collection.Add(new DummyClass(){
                name = "A2",
                value = 4,
                status = "I"
            });
            _collection.Add(new DummyClass(){
                name = "A2",
                value = 5,
                status = "A"
            });
            _collection.Add(new DummyClass(){
                name = "A3",
                value = 6,
                status = "I"
            });
            _collection.Add(new DummyClass()
            {
                name = "A3",
                value = 7,
                status = "I"
            });
        }

        public List<DummyClass> GetCollection()
        {
            return _collection;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var dcc = new DummyClassCollection();
            var collection = dcc.GetCollection();

            var result = collection.GroupBy(g => g.name)
                                   .ToDictionary(d => d.Key, d => (d.OrderBy(or => or.status).First()));
            Console.ReadKey();
        }
    }
}