2016-07-30 9 views
0

Ich habe diese DatentabelleConvert Datatable zu baumartiger Klassenstruktur

Name Amount Category 
sample 100.00 1 
sample 100.00 1 
aasdas 11.00 1 
asd  1.00 2 
sadjas 1.00 2 
sadjas 1.00 2 
asdasd 1.00 3 
asdasd 1.00 3 
test 10.00 3 

Und ich habe diese Klasse

public class ProductListModel 
{ 
    public string CategoryName { get; set; } 
    public List<ProductModel> prodList { get; set; } 
} 

und jede Instanz dieser Klasse enthalten mit dieser Definition eine Liste von Produktmodellen

public class ProductModel 
{ 
    public string Name{ get; set; } 
    public double Amount { get; set; } 
} 

Wie kann ich diese Datentabelle konvertieren, um auf die Definition vonzu passenKlasse?

So würde die Ausgabe in dieser Form (für die Visualisierung nur):

// Group by category 1 
-- ProductListModel 
    --CategoryName = 1 
    -- ProdList 
     -- Name = sample, amount = 100.00  
     -- Name = sample, amount = 100.00  
     -- Name = aasdas, amount = 11.00 

// Group by category 2 
-- ProductListModel 
    --CategoryName = 2 
    -- ProdList 
     -- Name = asd, amount = 1.00  
     -- Name = sadjas, amount = 1.00  
     -- Name = sadjas, amount = 1.00  

// Group by category 3  
-- ProductListModel 
    --CategoryName = 3 
    -- ProdList 
     -- Name = asdasd, amount = 1.00  
     -- Name = asdasd, amount = 1.00  

Ich habe versucht, aber ich bin fest: P

Antwort

1

Sie können es so "vereinfachen"

var productList = datatable.Rows.Cast<DataRow>() 
    .GroupBy(r => r["Category"] + "").Select(g => new ProductListModel { 
     CategoryName = g.Key, 
     prodList = g.Select(r => new ProductModel { 
      Name = r["Name"] + "", 
      Amount = (double)r["Amount"] 
     }).ToList() 
    }).ToList(); 
+0

das ist was ich suche! Vielen Dank – Sherlock

1

das es sein könnte? Dadurch werden die Kategorien in eine Liste eingefügt und die entsprechenden Produkte in die Kategorien eingefügt. (Dies ist jedoch nicht getestet)

List<ProductListModel> productCategories = new List<ProductListModel>(); 

foreach (DataRow row in dataTable.Rows) // Go through all data rows 
{ 
    if (!productCategories.Any(pc => pc.CategoryName == (string)row[2])) // if the product's category is not registered 
     productCategories.Add(new ProductListModel() { CategoryName = (string)row[2] }); // Then add the category to the category list 

    // Add the current product (row) to the first registered category that matches the product's own 
    productCategories.First(pc => pc.CategoryName == (string)row[2]).prodList.Add(new ProductModel() 
    { 
     Name = (string)row[0], 
     Amount = double.Parse((string)row[1]) 
    }); 
} 

Ich hoffe, das hilft.

bearbeiten

änderte ich den Code ein wenig, so dass sich wiederholende Produkte ihre ammouts gestapelt erhalten wird (wenn das Sinn macht):

List<ProductListModel> productCategories = new List<ProductListModel>(); 

foreach (DataRow row in dataTable.Rows) 
{ 
    if (!productCategories.Any(pc => pc.CategoryName == (string)row[2])) 
     productCategories.Add(new ProductListModel() { CategoryName = (string)row[2] }); 

    ProductListModel currentCategory = productCategories.First(pc => pc.CategoryName == (string)row[2]); 

    if (currentCategory.prodList.Any(pr => pr.Name == (string)row[0])) 
     currentCategory.prodList.First(pr => pr.Name == (string)row[0]).Amount += double.Parse((string)row[1]); 
    else 
     currentCategory.prodList.Add(new ProductModel() { Name = (string)row[0], Amount = double.Parse((string)row[1]) }); 
} 
+0

für den Aufwand, aber es funktioniert nicht, kann dies ohne Schleifen gemacht werden? – Sherlock

+0

Wie funktioniert es nicht? Was läuft schief? Was ist das Ergebnis gegenüber dem, was Sie erwarten? Was ist falsch mit Schleifen? – MasterXD

+0

@Sherlock Ich habe einige Änderungen am Code vorgenommen. Vielleicht hast du das nicht verstanden, als du den Code kopiert hast. Ich habe auch eine andere Version gemacht. – MasterXD

0

ich es wie dieses

List<ProductListModel> productList = new List<ProductListModel>(); 

foreach (var row in datatable.AsEnumerable()) 
{ 
    var categoryId = row.Field<string>("Category"); 
    var product = new ProductModel 
    { 
     Name = row.Field<string>("Name"), 
     Amount = row.Field<double>("Amount") 
    }; 

    // Build each category 
    if (!productList.Any(x => x.CategoryName == categoryId)) 
    { 
     var itemList = new List<ProductModel>(); 

     itemList.Add(product); 
     productList.Add(new ProductListModel { CategoryName = categoryId, prodList = itemList }); 
    } 
    else 
    { 
     var existingprodList = productList.Where(x => x.CategoryName == categoryId).FirstOrDefault(); 
     existingprodList.prodList.Add(product); 
    } 
} 

gelöst