Linq GroupBy to Class

时间:2013-12-18 13:56:02

标签: c# linq

所以,我有以下课程:

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

现在我希望将它们作为Catgory列表。

我可以遍历此列表以创建父亲列表,但我想要做的是使用linq“group by”或任何其他方法来实现此目的。

这可能吗?

3 个答案:

答案 0 :(得分: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()
    })

答案 1 :(得分: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;
        }
    }

答案 2 :(得分: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();