2009-06-10 18 views
0

Mit meinem folgenden Code:Gruppierung - Linq to Objects

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

namespace SampleGrouping 
{ 
internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     var sample = new List<Samples> 
            { 
             new Samples{ParameterID = 11,MaterialID = 855,ProductID = 955,Quantity = 100}, 
             new Samples{ParameterID = 11,MaterialID = 855,ProductID = 955,Quantity = 200}, 
             new Samples{ParameterID = 12,MaterialID = 856,ProductID = 956,Quantity = 100}, 
             new Samples{ParameterID = 12,MaterialID = 856,ProductID = 956,Quantity = 400} 
            }; 
     // Result: Groupby ParameterID, MaterialID, ProductID 
     // ParameterID = 11,MaterialID = 855,ProductID = 955,Quantity = 300 
     // ParameterID = 12,MaterialID = 856,ProductID = 956,Quantity = 500 

     var enumerable = from s in sample 
         group sample by new 
              { 
               s.ParameterID, 
               s.MaterialID, 
               s.ProductID, 

              }; 
    } 
} 

internal class Samples 
{ 
    public int MaterialID { get; set; } 
    public int ParameterID { get; set; } 
    public int ProductID { get; set; } 
    public int Quantity { get; set; } 
} 
} 

Wie ich Ergebnis wie erreichen kann: Liste als

ParameterID = 11, MaterialID = 855, ProductID = 955, Anzahl = 300
ParameterID = 12, MaterialID = 856, ProductID = 956, Anzahl = 500

Dank

Antwort

3

Gruppe durch "s", anstelle von "Probe". Dann können Sie die Daten auswählen, wie Sie es brauchen:

+0

Danke, dass es funktioniert hat. – Sreedhar