pnet
pnet

Reputation: 268

I get an error because I can't instantiate an object inside another

I looked for an answer for my issue in this site but I couldn't find anything.

My earlier post was very long, so i'll try to improve it.

I have this model:

public class Product : EntityBase
    {
        public string Name { get; set; }
        //attribuitions
        ....
        //Url das images?

        public int? CategoryId { get; set; }
        public virtual Picture Picture { get; set; }
        //More attribuitions
        ....
}

My need is to load a property from mode but this property is a virtual property and i'm not geting to instantiate in my lambda. I did

var qry = _productRepository.Table.GroupJoin(_categoriesRepository.Table,            
            p => p.CategoryId,
            c => c.Id,
            (p, c) => new { Product = p, Categories = c.DefaultIfEmpty() })
            .Where(hdg => hdg.Product.Hidden == false)

            .SelectMany(final => final.Categories,
            (final, c) => new CatalogItemResponse
            {
                ChildrenCategoryId = final.Product.ChildrenCategoryId,

                DolarRate = 0.0m,
                ResellerPriceUSD = 0.0m,
                ResellerPriceBRL = 0.0m,
                BasePriceBRL = 0.0m,
                BasePriceUSD = 0.0m,

                CategoryId = final.Product.CategoryId,
                CategoryName = (c != null ? c.Name : null),
                PictureId = final.Product.PictureId,
                Description = final.Product.Description,
                ShortDescription = final.Product.ShortDescription,
                Name = final.Product.Name,
                NameHtml = string.IsNullOrEmpty(final.Product.NameHtml) ? final.Product.Name : final.Product.NameHtml,
                PartNumber = final.Product.PartNumber,
                Hidden = final.Product.Hidden,
                Order = final.Product.Order,
                HaveMaximumPercentage = final.Product.HaveMaximumPercentage,
                MaximumPercentage = final.Product.MaximumPercentage,
                HaveMinimumPercentage = final.Product.HaveMinimumPercentage,
                MinimumPercentage = final.Product.MinimumPercentage,
                AuthorizeMaximumPercentageAlteration = final.Product.AuthorizeMaximumPercentageAlteration,
                AuthorizeMinimumPercentageAlteration = final.Product.AuthorizeMinimumPercentageAlteration,
                StandardMarkup = final.Product.StandardMarkup,
                DistributionCenterErpId = final.Product.DistributionCenterErpId,
                PictureFilename = final.Product.Picture.FileName

            }).ToList();

            qry.ForEach(q =>
            {                
                var product = new Product();

                product.CategoryId = q.CategoryId;
                product.AuthorizeMaximumPercentageAlteration = q.AuthorizeMaximumPercentageAlteration;
                product.AuthorizeMinimumPercentageAlteration = q.AuthorizeMinimumPercentageAlteration;
                product.HaveMaximumPercentage = q.HaveMaximumPercentage;
                product.HaveMinimumPercentage = q.HaveMinimumPercentage;
                product.Hidden = q.Hidden;
                product.ChildrenCategoryId = q.ChildrenCategoryId;
                product.Description = q.Description;
                product.DistributionCenterErpId = q.DistributionCenterErpId;
                product.MaximumPercentage = q.MaximumPercentage;
                product.MinimumPercentage = q.MinimumPercentage;
                product.Name = q.Name;
                product.NameHtml = q.NameHtml;
                product.Order = q.Order;
                product.PartNumber = q.PartNumber;
                product.PictureId = q.PictureId;
                product.ShortDescription = q.ShortDescription;
                product.StandardMarkup = q.StandardMarkup;

                var parentProducts = _productService.GetParentsOf(product.Id).Select(x => x.PartNumber);
                q.Parents.AddRange(parentProducts);

               //if (product.PictureId.HasValue)
                    //q.PictureFilename = product.Picture.FileName;

                var price = _erpPriceService.GetPrice(product, 1, resellerId).Result;

                if (price.BasePriceUSD > 0)
                    q.DolarRate = price.BasePriceBRL / price.BasePriceUSD;

                q.ResellerPriceUSD = price.ResellerPriceUSD;
                q.ResellerPriceBRL = price.ResellerPriceBRL;
                q.BasePriceBRL = price.BasePriceBRL;
                q.BasePriceUSD = price.BasePriceUSD;

            });

But when i run the code above in the commented lines i get an exception of NullReference. It's happening due Picture i can not instantiate it. I did a left join below .Where(First query) and nothing. I tried to put .Include this way: _productRepository.Table.Include(pic => pic.Picture).GroupJoin and nothing. I tried .Include after .Where and nothing. In other words, i did many attempts and i didn't get. I tried a anonymous e nothing. What do i do? What wrong am I doing?

EDIT1

If i put .Include like this

var qry = _productRepository.Table.Include(pic => pic.Picture).GroupJoin(_categoriesRepository.Table,
            p => p.CategoryId,
            c => c.Id,
            (p, c) => new { Product = p, Categories = c.DefaultIfEmpty() })
            .Where(hdg => hdg.Product.Hidden == false)

            .SelectMany(final => final.Categories,
            (final, c) => new CatalogItemResponse
            {
                ChildrenCategoryId = final.Product.ChildrenCategoryId,
                ................... //Attribuitions below

I think that i don't need new Product() inside qry.ForEach, correct? Then how do i do, do i need to do a new Product()? Putting an include i don't know what to do or how.

Upvotes: 0

Views: 107

Answers (1)

Ahmad Alfawwaz Timehin
Ahmad Alfawwaz Timehin

Reputation: 303

Check that you are setting the Picture in your ForEach. Although the problem is most likely a result of you not including the Picture entity in your query for Product. Try adding it to the GroupJoin on the condition that Picture.Id == Product.PictureId.

You could also use the more convenient .Include to eager load the Picture entity.

Upvotes: 1

Related Questions