2011-06-10 32 views
9

Bu soruna çok zaman harcadım.LINQ Group, birden fazla özellik gösteren VB.Net

dim lFinal={new with {.Year=2010, .Month=6, .Value1=0, .Value2=0}, 
      new with {.Year=2010, .Month=6, .Value1=2, .Value2=1}, 
      new with {.Year=2010, .Month=7, .Value1=3, .Value2=4}, 
      new with {.Year=2010, .Month=8, .Value1=0, .Value2=1}, 
      new with {.Year=2011, .Month=1, .Value1=2, .Value2=2}, 
      new with {.Year=2011, .Month=1, .Value1=0, .Value2=0}} 

Dim lFinal2 = From el In lFinal 
       Group el By Key = new with {el.Year,el.Month} 
       Into Group 
       Select New With {.Year = Key.Year, .Month=Key.Month, .Value1 = Group.Sum(Function(x) x.Value1), .Value2 = Group.Sum(Function(x) x.Value2)} 

lFinal.Dump() 
lFinal2.Dump() 

lFinal: Ben (bir mülkiyet) LINQ sorguları tarafından basit Grup yapmak mümkün ama birden alanlar için biraz şaşırıp ... İşte yapmak istediğim şeyin bir LINQPad örneğidir liste 6 maddeden oluşuyor, lFinal2'nin 4 maddeye sahip olmasını istiyorum: 2010-6 ve 2011-1 gruplandırılmalıdır.

Şimdiden teşekkürler.

+0

Ayrıca ha d çalışmasını sağlamak için GetHashCode() uygulamak için. – Maher

cevap

4

% 100 emin ama grup muhtemelen Eşittir kullanır tarafından() ve/veya GetHashCode değil uygulama, bu nedenle örtülü oluşturulmasını yaptığınızda:

= Group el By Key = new with {el.Year,el.Month} 

örtük nesne hem yıl kontrol etmek bilmez ve Ay (sadece özellikleri olduğundan diğer nesnelerle karşılaştırırken onları kontrol ettiği anlamına gelmez).

Yani muhtemelen daha böyle bir şey yapmak gerekir:

= Group el By Key = new CustomKey() { Year = el.Year, Month = el.Month }; 

public class CustomKey{ 
    int Year { get; set; } 
    int Month { get; set; } 

    public override bool Equals(obj A) { 
     var key (CustomKey)A; 
     return key.Year == this.Year && key.Month == this.Month; 
    } 
} 
+0

+1. OP'nin sözdizimi LINQ-to-SQL ile iyi çalışacak olsa da, LINQ-to-Objects üzerinde kullanıldığında geçerli çıktıyı üretecektir. –

+1

+0 Bu doğru olsa da, 'Key' anahtar kelimesi doğru cevaptır. –

5

Teşekkür! Ancak, çalışmasını sağlamak için GetHashCode işlevini de yazmam gerektiğini fark ettim.

Sınıf:

Public Class YearMonth 
    Implements IEquatable(Of YearMonth) 

    Public Property Year As Integer 
    Public Property Month As Integer 

    Public Function Equals1(other As YearMonth) As Boolean Implements System.IEquatable(Of YearMonth).Equals 
     Return other.Year = Me.Year And other.Month = Me.Month 
    End Function 

    Public Overrides Function GetHashCode() As Integer 
     Return Me.Year * 1000 + Me.Month * 100 
    End Function 
End Class 

Ve LINQ sorgusu:

Dim lFinal2 = From el In lFinal 
       Group el By Key = New YearMonth With {.Year = el.Year, .Month = el.Month} 
       Into Group 
       Select New ItemsByDates With {.Year = Key.Year, 
              .Month = Key.Month, 
              .Value1 = Group.Sum(Function(x) x.Value1), 
              .Value2 = Group.Sum(Function(x) x.Value2)} 
+1

+0 Bu doğru olsa da, 'Anahtar' anahtar sözcüğü doğru cevaptır. –

18

kullanarak anonim tip özellikler değişmez olun ben VB.Net son tertip + LINQ GroupBy tercümesini sağlayabilir Key anahtar kelime daha sonra karşılaştırmak için kullanılacaktır

Dim lFinal2 = From el In lFinal    
    Group el By Key = new with {key el.Year, key el.Month} 
    Into Group 
    Select New With { 
      .Year = Key.Year, 
      .Month = Key.Month, 
      .Value1 = Group.Sum(Function(x) x.Value1), 
      .Value2 = Group.Sum(Function(x) x.Value2) 
    } 
İlgili konular