public Category GetWithProducts(string categoryName)
        {
            using (var cn = WJStoreConnection)
            {
                var dr = cn.ExecuteReader(
                    "SELECT * " +
                    "  FROM Category G" +
                    "  JOIN Product A ON A.CategoryId = G.CategoryId" +
                    " WHERE G.Name = @Name", new {Name = categoryName});

                Category category = null;
                while (dr.Read())
                {
                    if (category == null)
                    {
                        category = new Category
                        {
                            CategoryId = Convert.ToInt32(dr["CategoryId"]),
                            Name = dr["Name"].ToString(),
                            Description = dr["Description"].ToString()
                        };
                    }

                    category.Products.Add(new Product
                    {
                        ProductId = Convert.ToInt32(dr["ProductId"]),
                        Title = dr["Title"].ToString(),
                        Price = Convert.ToDecimal(dr["Price"]),
                        ProductArtUrl = dr["ProductArtUrl"].ToString(),
                        CategoryId = Convert.ToInt32(dr["CategoryId"]),
                        Category = category
                    });
                }
                return category;
            }
        }
Example #2
0
 public static CategoryMenuViewModel ToViewModel(Category category)
 {
     return Mapper.Map<CategoryMenuViewModel>(category);
 }