quangdd5757 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

dapper not loading child object properties #148

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

I have to load parent child hierarchy object using [dapper dot net][1]

i am using northwind db tables (category and products)

string query = "select c.categoryid, c.categoryname , p.productid, 
p.productname, p.categoryid from products p join Categories c on c.categoryid = 
p.categoryid"

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();

                IEnumerable<Category> categories = con.Query<Category, Product, Category>(query,
                                        (c, p) => { return GetCategory(c, p); },
                                        splitOn: "categoryid");

                foreach (var item in categories.Distinct())
                {
                    Console.WriteLine(string.Format("CategoryID: {0}, Category Name: {1}, Products Count: {2}",
                        item.CategoryID, item.CategoryName, item.Products.Count));
                    foreach (Product prod in item.Products)
                    {
                        Console.WriteLine(string.Format("\tProductID: {0}, Product Name: {1}", prod.ProductID, prod.ProductName));
                    }
                }

                con.Close();
            }

        static Dictionary<int, Category> categoryLookup = new Dictionary<int, Category>();
        static Category GetCategory(Category possibleDupeCategory, Product product)
        {
            Category category;

            if (!categoryLookup.TryGetValue(possibleDupeCategory.CategoryID, out category))
            {
                categoryLookup.Add(possibleDupeCategory.CategoryID, possibleDupeCategory);

                category = possibleDupeCategory;
            } GetCategory

            category.Products.Add(product);
            product.Category = category;
            return category;
        }`

**Issue:**
- properties of prouduct object in GetCategory method are null except the 
productId, i have checked the properties names( so don't worry about that)

my dapper version is 1.13

Original issue reported on code.google.com by SaboorAh...@gmail.com on 18 Jul 2013 at 7:20