2010-06-23 10 views
18
from x in myCollection 
    group x by x.Id into y 
    select new { 
     Id = y.Key, 
     Quantity = y.Sum(x => x.Quantity) 
    }; 

Come scriveresti sopra come espressione lambda? Sono bloccato sulla parte group into.GroupBy in lambda expressions

risposta

39

continuazioni query (SELECT ... INTO e di gruppo ... in, ma non unirsi ... in) sono pari a poco frazionato l'espressione di query. Così mi piace pensare a vostro esempio come:

var tmp = from x in myCollection 
      group x by x.Id; 
var result = from y in tmp 
      select new { 
       Id = y.Key, 
       Quantity = y.Sum(x => x.Quantity) 
      }; 

cambiare quelle in notazione:

var tmp = myCollection.GroupBy(x => x.Id); 
var result = tmp.Select(y => new { 
       Id = y.Key, 
       Quantity = y.Sum(x => x.Quantity) 
      }); 

allora si potrebbe combinare di nuovo:

var tmp = myCollection.GroupBy(x => x.Id) 
         .Select(y => new { 
           Id = y.Key, 
           Quantity = y.Sum(x => x.Quantity) 
           }); 

Una volta che si lavora fuori ciò che il Il compilatore C# fa con le espressioni di query, il resto è relativamente semplice :)

5
myCollection 
    .GroupBy(x => x.Id) 
    .Select(x => 
     new 
     { 
      Id = x.Key, 
      Quantity = x.Sum(y => x.Quantity 
     }); 
7
myCollection.GroupBy(x => x.Id) 
      .Select(y => new { 
           Id = y.Key, 
           Quantity = y.Sum(x => x.Quantity) 
          }); 
1
 var mostFrequent = 
      lstIn.Where(i => !string.IsNullOrEmpty(i)) 
       .GroupBy(s => s) 
       .OrderByDescending(g => g.Count()) 
       .Select(s => s.Key) 
       .FirstOrDefault(); 
+3

È necessario aggiungere del testo che spiega cosa fa questa risposta e perché fornisce una soluzione al problema specifico. I dump di codice sono raramente utilizzati da altre persone. –

-1

Quindi, per la maggior parte delle risposte qui, tutti sembrano avere a che fare con ottenere un oggetto semplice di ID fatto dal conteggio del gruppo e la Chiave stessa che è group.Key.

Anche se questo è probabilmente il principale utilizzo di questo. Non ha davvero soddisfatto i miei bisogni.

Per il mio caso, sostanzialmente volevo raggruppare per alcune proprietà dell'oggetto, quindi recuperare un oggetto specifico da quel gruppo. Ecco un codice di esempio.

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

public class Program 
{ 
    public static void Main() 
    { 
     Console.WriteLine("Hello World"); 
     var response = new List<ResponseClass>(); 
      var listOfStudents = new List<Student>(); 
      // Insert some objects into listOfStudents object. 
      listOfStudents.GroupBy(g => g.Class).ToList() 
       .ForEach(g => response.Add(g.OrderByDescending(s => s.CreatedOn).Select(a => 
       new ResponseClass 
       { 
        SName = a.StudentName, 
        SAge = a.Age, 
        SClass = a.Class, 
        SCreatedOn = a.CreatedOn, 
        RandomProperty = Guid.NewGuid().ToString() 
       }) 
       .First())); 

       Console.WriteLine("This compiles and should work just fine"); 
    } 
    class Student 
    { 
     public string StudentName { get; set; } 
     public int Age { get; set; } 
     public string Class { get; set; } 
     public DateTime CreatedOn { get; set; } 
    } 

    class ResponseClass 
    { 
     public string SName { get; set; } 
     public int SAge { get; set; } 
     public string SClass { get; set; } 
     public DateTime SCreatedOn { get; set; } 
     public string RandomProperty { get; set; } 
    } 
} 

Se si preferisce utilizzare un ciclo foreach (io preferisco lambda come trovo più facile da leggere), ma se lo fai, si poteva fare in questo modo.

Spero che questo aiuti qualcuno. :)