C#

2016-04-14 14 views
0

'daki bir koşulu temel alan bir Listeden numaralı listeden silme Her biri SomeId, AnotherId, SomeOtherId ve Timestamp öğelerine sahip bir nesne (aşağıdaki kod örneğinde exobject) var. Bu liste, her kaydın farklı bir Zaman Damgası'na sahip yinelenen girişleri olabilir. Bu nesnenin tüm yinelemelerini eski Zaman Damgaları ile kaldırmak ve yalnızca en son olanları tutmak istiyorum. C#

Numune nesnesi:

SomeId AnotherId SomeOtherId Timestamp 
1   2   1    10 
1   2   1    20 
1   3   2    30 
2   3   4    40 
1   3   2    50 

My gerekli liste

1,2,1,20 and 1,3,2,50 and 2,3,4,40. 

Bunu yapmak C# çok kaba uygulama var olmalıdır.

for (int i = 0; i < exObject.Count - 1; i++) 
{ 
    for (int j = i + 1; j < exObject.Count - 1; j++) 
    { 
     if (exObject[i].SomeId == exObject[j].SomeId && exObject[i].AnotherId == exObject[j].AnotherId && exObject[i].SomeOtherId == exObject[j].SomeOtherId) 
     { 
      if (exObject[i].TimeStamp < exObject[j].TimeStamp) 
       exObject[i].TimeStamp = exObject[j].TimeStamp; 
      exObject.Remove(exObject[j]); 
     } 
    } 
} 
Orada bunu yapmak için daha zarif ve daha hoş bir yoludur ya olmadığını bilmek istiyorum

Bunu gerçekleştirmek için kullanabileceğiniz bir lambda varsa.

+0

exObject.Distinct() kullanmak istemiyor musunuz? – Damirchi

+0

Şimdi Farklı yaklaşımı deniyorum. – yazz

cevap

1

System.Linq Bir Farklı bir yöntem vardır. Bir IEqualityComparer uygulamak zorunda. arasında nasıl burada Detayları ... Yorumunuza dayalı

https://msdn.microsoft.com/en-us/library/bb338049(v=vs.110).aspx


Düzenleme: Bir orderBy yaparsanız o ... Burada bazı kod ... istediğiniz birini tutmalı

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var data = new[] 
      { 
       new SomeClass { SomeId = 1, AnotherId = 1, SomeOtherId = 1, Timestamp = 10 }, 
       new SomeClass { SomeId = 1, AnotherId = 1, SomeOtherId = 1, Timestamp = 20 }, // Duplicate 
       new SomeClass { SomeId = 1, AnotherId = 2, SomeOtherId = 2, Timestamp = 30 }, 
       new SomeClass { SomeId = 1, AnotherId = 2, SomeOtherId = 2, Timestamp = 35 }, // Duplicate 
       new SomeClass { SomeId = 2, AnotherId = 4, SomeOtherId = 4, Timestamp = 40 }, 
       new SomeClass { SomeId = 3, AnotherId = 2, SomeOtherId = 2, Timestamp = 50 }, 
       new SomeClass { SomeId = 1, AnotherId = 1, SomeOtherId = 1, Timestamp = 50 } // Duplicate 
      }; 

      var distinctList = data 
         .OrderBy(x => x.Timestamp) 
         .Distinct(new SomeClassComparer()) 
         .ToList(); 
      } 

     public class SomeClass 
     { 
      public int SomeId { get; set; } 
      public int AnotherId { get; set; } 
      public int SomeOtherId { get; set; } 
      public int Timestamp { get; set; } 
     } 

     public class SomeClassComparer : IEqualityComparer<SomeClass> 
     { 
      public bool Equals(SomeClass x, SomeClass y) 
      { 
       if (ReferenceEquals(x, y)) 
       { 
        return true; 
       } 

       //Check whether any of the compared objects is null. 
       if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) 
       { 
        return false; 
       } 

       //Check whether the SomeClass's properties are equal. 
       return x.SomeId == y.SomeId && 
         x.AnotherId == y.AnotherId && 
         x.SomeOtherId == y.SomeOtherId; 
      } 

      public int GetHashCode(SomeClass someClass) 
      { 
       //Check whether the object is null 
       if (ReferenceEquals(someClass, null)) 
       { 
        return 0; 
       } 

       //Get hash code for the fields 
       var hashSomeId = someClass.SomeId.GetHashCode(); 
       var hashAnotherId = someClass.AnotherId.GetHashCode(); 
       var hashSomeOtherId = someClass.SomeOtherId.GetHashCode(); 

       //Calculate the hash code for the SomeClass. 
       return (hashSomeId^hashAnotherId)^hashSomeOtherId; 
      } 
     } 
    } 
} 
+0

Bunu uygulamayı denedim, ancak koşulu zaman damgasına nasıl eklerim? Daha eski bir zaman damgası olan kaydı kaldırmak için? – yazz

+0

Bu, şu andaki çalışma uygulamamla benzer. Çok teşekkürler. – yazz

0
3 alanlarına göre

yapabilirsiniz Grubu ve her bir grubun ilk birini alabilir: mükemmel çalışıyor

List 
    .GroupBy(x=> new {x.prop1, x.prop2, x.prop3 }) 
    .Select(g=> g.OrderByDescending(o=> o.dateprop).First()) 
    .ToList(); 

Örnek:

static void Main(string[] args) 
{ 
    List<Foo> myList = new List<Foo>(); 
    myList.Add(new Foo(1, 2, 1, 10)); 
    myList.Add(new Foo(1, 2, 1, 20)); 
    myList.Add(new Foo(1, 3, 2, 30)); 
    myList.Add(new Foo(2, 3, 4, 40)); 
    myList.Add(new Foo(1, 3, 2, 50)); 

    // The following returns 3 results with 20, 50 and 40 timeStamps. 

    var results = myList.GroupBy(x => new { x.SomeId, x.AnotherId, x.SomeOtherId }) 
          .Select(g => g.OrderByDescending(o => o.Timestamp).First()).ToList(); 

} 
+0

Birincisi, kayıtların ilk ortaya çıkışı olacak. Ancak en son zaman damgası ile kayıt olmayabilir. Daha eski bir zaman damgalı bir kayıt daha sonra listede bulunabilir. Ben lambda için yeni - GroupBy ve OrderByDesc bir arada var mı? Lütfen bir kod snippet'i sağlayın. – yazz

+0

Ardından tarihe göre sipariş verebilir, daha sonra ilk önce – user3185569

+0

numaralı telefonu tekrar kontrol edin. – user3185569