2013-12-18 124 views
0

所以,我有以下類:的LINQ的GroupBy類

public class Category 
{ 
    public int idCategory { get; set; }; 
    public string nameCategory { get; set; }; 
    public List<Product> products { get; set; } 
} 

public class Product 
{ 
    public int idProduct { get; set; }; 
    public string nameProduct { get; set; }; 
    public float price { get; set; }; 
} 

我有返回包含在這條路上元素列表的方法:

idCategory, nameCategory, idProduct, nameProduct, price 
0, Shoes, 0, Jordan, 99.9 
0, Shoes, 1, Nike, 59.9 
0, Shoes, 2, Adidas, 85.6 
0, Shoes, 3, NewFeel, 11.0 
1, watch, 6, Armani, 59.9 
1, watch, 8, CK, 85.6 
1, watch, 9, Rolex, 11.0 

現在我想,讓他們作爲類別列表。

我可以通過這個列表來循環創建一個父親列表,但我想要做的就是使用linq「group by」或任何其他方法來實現此目的。

這是可能的嗎?

+1

請在你所期望的更多的描述。 Acutally給我們一些虛擬數據和你的期望。謝謝 ! – Christos

+0

由於您的列表中有一系列對象,因此'GroupBy()'方法僅將這些對象分組,而不將這些對象的內容分組。 – Transcendent

+0

讓你的方法返回列表難道不是更容易嗎? – tinstaafl

回答

1

是這樣的嗎?

flatList 
    .GroupBy(i => i.FatherId) 
    .Select(g => new Father() 
    { 
     id = g.Key, 
     propertyF, 
     children = 
      g 
       .Select(c => new Child() 
       { 
        id = ChildrenId, 
        prop1, 
        prop2 
       }) 
       .ToList() 
    }) 
+0

感謝這正是我所需要的(y) – MSTr

0
fathers.SelectMany(f => f.children, 
         (f, c) => new YourConsolidatedClassName 
            { 
             Id = f.Id, 
             propertyF = f.propertyF, 
             ChildId = c.Id, 
             prop1 = c.prop1, 
             prop2 = c.prop2 
            }).ToList(); 
+0

感謝您的重新格式化。 – David

1

如果你有麻煩讓與您提供的這項工作,這裏是一個另類:

List<string> input = new List<string> 
{ 
    "idCategory,nameCategory,idProduct,nameProduct,price", 
    "0,Shoes,0,Jordan,99.9", 
    "0,Shoes,1,Nike,59.9", 
    "0,Shoes,2,Adidas,85.6", 
    "0,Shoes,3,NewFeel,11.0", 
    "1,watch,6,Armani,59.9", 
    "1,watch,8,CK,85.6", 
    "1,watch,9,Rolex,11.0", 
}; 
List<Category> categories = new List<Category>(); 
foreach(string s in input.Skip(1)) 
{ 
    string[] temp = s.Split(','); 
    if(categories.Contains<Category>(new Category 
    { 
     idCategory = int.Parse(temp[0]) 
    }, new CategoryEquality())) 
     categories.Find(id => id.idCategory == int.Parse(temp[0])).products.Add(new Product 
     { 
      idProduct = int.Parse(temp[2]), 
      nameProduct = temp[3], 
      price = float.Parse(temp[4]) 
     }); 
    else 
     categories.Add(new Category 
     { 
      idCategory = int.Parse(temp[0]), 
      nameCategory = temp[1], 
      products = new List<Product> 
      { new Product 
       { 
        idProduct = int.Parse(temp[2]), 
        nameProduct = temp[3], 
        price = float.Parse(temp[4]) 
       } 
      } 
     }); 
} 
    public class CategoryEquality : IEqualityComparer<Category> 
    { 
     public bool Equals(Category a, Category b) 
     { 
      return a.idCategory == b.idCategory; 
     } 
     public int GetHashCode(Category a) 
     { 
      return a.idCategory.GetHashCode(); 
     } 
    } 
    public class Category 
    { 
     public int idCategory 
     { 
      get; 
      set; 
     } 
     public string nameCategory 
     { 
      get; 
      set; 
     } 
     public List<Product> products 
     { 
      get; 
      set; 
     } 
    } 

    public class Product 
    { 
     public int idProduct 
     { 
      get; 
      set; 
     } 
     public string nameProduct 
     { 
      get; 
      set; 
     } 
     public float price 
     { 
      get; 
      set; 
     } 
    } 
+0

謝謝你,我喜歡你的代碼它簡單易懂,但我正在尋找像「D斯坦利」答案,它工作正常。 – MSTr