2016-07-15 8 views
0

Wie summiere Bedingung in Array Liste mit Klasse?Wie summiere Bedingung in Array Liste <class>? Unity C#

Zum Beispiel:

Ich habe diese Klasse:

productMerchant.cs

using UnityEngine; 
using System.Collections; 

public class productMerchant { 
    public int productID; 
    public string productName; 
    public int qty; 
    public int price; 
    public int have; 

    public productMerchant (int productid, string productname, int qtyx, int pricex, int havex) { 
     this.productID = productid; 
     this.productName = productname; 
     this.qty = qtyx; 
     this.price = pricex; 
     this.have = havex; 
    } 
} 

und ich habe dies:

public List<productMerchant> productMargaretSell = new List<productMerchant>(); 

So, wie Bedingung wie Summe < productMargaretSell.qty> die < productMargaretSell.productID = 10> zum Beispiel summieren.

Wenn es 3 producID ist = 10 mit VE = 2, 3, 1

So in Array Alle ProduktID = 10 Menge wird summieren. Und das Ergebnis muss sein:

ProduktID = 10 (Anzahl = 6)

ohne Looping mit dem ProduktID zu überprüfen, ob gleichen und dann summieren.

Da Schleifen verwendet werden, wenn Daten zu viele Daten sind, wird es verzögert.

Vielleicht gibt es einige Funktionen von der Einheit gebaut C# kann es tun?

Gefällt Ihnen dieses ProduktMargaretSell.Sum()?

Dank

+0

ich irgendwie klingt wie Sie in Wörterbücher interessieren: https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx – Zze

Antwort

0

dies kann leicht mit Linq erfolgen.

List<productMerchant> result = productMargaretSell 
    .GroupBy(l => l.productID) 
    .Select(cl => new productMerchant 
      { 
       productID = cl.First().productID, 
       productName= cl.First().productName, 
       qty= cl.Sum(c => c.qty).ToString(), 
      }).ToList(); 
0

Das Schlüsselwort ist GroupBy

public class productMerchant 
    { 
     public int productID { get; set; } 
     public string productName { get; set; } 
     public int qty { get; set; } 
     public int price { get; set; } 
     public int have { get; set; } 

     public productMerchant(int productid, string productname, int qtyx, int pricex, int havex) 
     { 
      this.productID = productid; 
      this.productName = productname; 
      this.qty = qtyx; 
      this.price = pricex; 
      this.have = havex; 
     } 
    } 

    public static void Main(string[] args) 
    { 
     List<productMerchant> productMerchants = new List<productMerchant>(); 
     productMerchants.Add(new productMerchant(10, "A", 1, 0, 0)); 
     productMerchants.Add(new productMerchant(10, "A", 2, 0, 0)); 
     productMerchants.Add(new productMerchant(10, "A", 3, 0, 0)); 

     productMerchants.Add(new productMerchant(11, "B", 4, 0, 0)); 
     productMerchants.Add(new productMerchant(11, "B", 5, 0, 0)); 
     productMerchants.Add(new productMerchant(11, "B", 6, 0, 0)); 

     //foreach (var productMerchant in productMerchants) 
     // Console.WriteLine(productMerchant.productName + " - " + productMerchant.productID + " - " + productMerchant.qty); 

     var results = productMerchants.GroupBy(g => g.productID) 
      .Select(x => new 
      { 
       id = x.Key, 
       sum = x.Sum(s => s.qty) 
      }); 

     foreach (var result in results) 
      Console.WriteLine(result.id + " - " + result.sum); 

    }