简体   繁体   English

通过子列表LINQ C#中的值区分行

[英]Distinct rows by their values from sub list LINQ C#

I would like to remove duplicate rows by their values from sub list using LINQ syntax. 我想使用LINQ语法从子列表中删除重复行的值。 Below I attached code which does that by different way. 下面我附上了以不同方式执行此操作的代码。

xxxxx xxxxxx xxxxxxx xxxxxxxxxxx xxxxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

List<ZRowCollection> zListCollection = new List<ZRowCollection>();
        zListCollection = zListCollection.OrderBy(p => p.P).ToList();
        int i1 = 1;
        foreach (var item in zListCollection.ToList())
        {
            var subList1 = item.XRowModified.Select(p => p).ToList();
            foreach (var item2 in zListCollection.Skip(i1).ToList())
            {
                var subList2 = item2.XRowModified.Select(p => p).ToList();
                int i = 0;
                foreach (var item3 in subList1)
                {
                    var t2 = subList2.Select(p => p.Average).ToList();
                    decimal average = t2[i];
                    if (item3.Average == average)
                    {
                        i++;
                    }
                    else break;
                }
                if (i == item2.XRowModified.Count)
                {
                    zListCollection.Remove(item2);
                }
            }
            i1++;
        }

properties 属性

class XRowModified
{
    public decimal Id { get; set; }
    public decimal Open { get; set; }
    public decimal High { get; set; }
    public decimal Low { get; set; }
    public decimal Close { get; set; }
    public DateTime Time { get; set; }
    public decimal Average { get; set; }
}
class ZRowCollection
{
    public ZRowCollection()
    {
        this.XRowModified = new HashSet<XRowModified>();
    }
    public int P { get; set; }
    public int High { get; set; }
    public int Low { get; set; }
    public virtual ICollection<XRowModified> XRowModified { get; set; }
}

expected input/output, as a comparer column Average in List<XRowModified> 预期的输入/输出,作为比较器列List<XRowModified>平均值

List<ZRowCollection> zListInput = new List<ZRowCollection>(){
            new ZRowCollection(){P = 0,High = 4,Low = 0, XRowModified = new List<XRowModified>(){
                    new XRowModified(){ Id = 1550, Open = 1.22M,High = 1.24M,Low = 1.21M,Close = 1.23M,Average = 1.225M,
                        Time = new DateTime(2012, 11, 9, 12, 23, 23, 222)},
                    new XRowModified(){ Id = 1551, Open = 1.20M,High = 1.24M,Low = 1.22M,Close = 1.20M,Average = 1.23M,
                        Time = new DateTime(2012, 11, 9, 12, 23, 25, 122)}}},
            new ZRowCollection(){P = 1,High = 3,Low = 0, XRowModified = new List<XRowModified>(){
                    new XRowModified(){ Id = 1555, Open = 1.22M,High = 1.24M,Low = 1.21M,Close = 1.23M,Average = 1.225M,
                        Time = new DateTime(2012, 11, 9, 12, 23, 40, 422)},
                    new XRowModified(){ Id = 1556, Open = 1.20M,High = 1.25M,Low = 1.20M,Close = 1.20M,Average = 1.23M,
                        Time = new DateTime(2012, 11, 9, 12, 23, 46, 522)}}},
            new ZRowCollection(){P = 2,High = 2,Low = 0, XRowModified = new List<XRowModified>(){
                    new XRowModified(){ Id = 1558, Open = 1.22M,High = 1.24M,Low = 1.21M,Close = 1.23M,Average = 1.225M,
                        Time = new DateTime(2012, 11, 9, 12, 30, 11, 622)},
                    new XRowModified(){ Id = 1559, Open = 1.20M,High = 1.24M,Low = 1.22M,Close = 1.20M,Average = 1.23M,
                        Time = new DateTime(2012, 11, 9, 12, 30, 12, 822)}}}
        };
        List<ZRowCollection> zListOutput = new List<ZRowCollection>(){
            new ZRowCollection(){P = 0,High = 4,Low = 0, XRowModified = new List<XRowModified>(){
                    new XRowModified(){ Id = 1550, Open = 1.22M,High = 1.24M,Low = 1.21M,Close = 1.23M,Average = 1.225M,
                        Time = new DateTime(2012, 11, 9, 12, 23, 23, 222)},
                    new XRowModified(){ Id = 1551, Open = 1.20M,High = 1.24M,Low = 1.22M,Close = 1.20M,Average = 1.23M,
                        Time = new DateTime(2012, 11, 9, 12, 23, 25, 122)}}}
        };

In this case I would consider writing a custom equality comparer, which could then be plugged in to the Distinct method. 在这种情况下,我将考虑编写一个自定义的相等比较器,然后可以将其插入Distinct方法中。 To do this you need two functions Equals and GetHashCode . 为此,您需要两个函数EqualsGetHashCode

Note: It is important the GetHashCode returns the same hash for two equal objects as this is the first thing that Distinct checks. 注意:重要的是, GetHashCode对于两个相等的对象返回相同的哈希,因为这是Distinct检查的第一件事。

From what I gather from your code two ZRow 's are equal if their XRow 's have the same sequence of averages, so our equality is that averageSequence1.SequenceEqual(averageSequence2) which would be implemented as so: 根据我从您的代码中收集到的信息,如果两个ZRowXRow具有相同的平均值序列,则它们相等,因此,我们的相等性是averageSequence1.SequenceEqual(averageSequence2) ,其实现方式如下:

public class CustomComparer : IEqualityComparer<ZRowCollection>
{
    public static CustomComparer Instance { get { return new CustomComparer(); } }

    Int32 IEqualityComparer<ZRowCollection>.GetHashCode(ZRowCollection value)
    {
        //One could also use the sum of the averages here, but went for simplicity...
        return value.XRowModified.Count;
    }

    Boolean IEqualityComparer<ZRowCollection>.Equals(ZRowCollection z1, ZRowCollection z2)
    {
        return z1.XRowModified.Select(x => x.Average)
                              .SequenceEqual(z2.XRowModified.Select(x => x.Average));
    }
}

and which would be used like so: 可以这样使用:

var distinctList = zListCollection.Distinct(CustomComparer.Instance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM