简体   繁体   English

我收到一个错误,因为我无法实例化另一个对象

[英]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. 我需要从mode加载一个属性,但是此属性是一个虚拟属性,我无法在我的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. 但是,当我在注释行中运行上面的代码时,我得到了NullReference的异常。 It's happening due Picture i can not instantiate it. 由于Picture我无法实例化它,因此发生了这种情况。 I did a left join below .Where(First query) and nothing. 我在.Where(First query)下没有进行左连接。 I tried to put .Include this way: _productRepository.Table.Include(pic => pic.Picture).GroupJoin and nothing. 我试图以这种方式放入.Include_productRepository.Table.Include(pic => pic.Picture).GroupJoin ,什么也没有。 I tried .Include after .Where and nothing. 我试图.Include之后.Where并没有什么。 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 EDIT1

If i put .Include like this 如果我放.Include包括这样

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? 我认为我在qry.ForEach中不需要new Product() ,对吗? Then how do i do, do i need to do a new Product() ? 那我该怎么办,我需要做一个new Product()吗? Putting an include i don't know what to do or how. 放置一个include我不知道该怎么办或如何做。

Check that you are setting the Picture in your ForEach . 检查是否在ForEach中设置了Picture Although the problem is most likely a result of you not including the Picture entity in your query for Product . 尽管问题很可能是由于您在对Product的查询中未包含Picture实体导致的。 Try adding it to the GroupJoin on the condition that Picture.Id == Product.PictureId . 尝试将它添加到GroupJoin条件是Picture.Id == Product.PictureId

You could also use the more convenient .Include to eager load the Picture entity. 您也可以使用更方便的.Includeeager load Picture实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能在函数中实例化对象 - Why can't I instantiate an object in a function 这是对适配器模式的正确使用吗? 我无法实例化该对象? - Is this a correct Use Of Adapter Pattern? I can't instantiate this object? 如何包含列表中的对象,另一个对象中的对象? - How can I include an object that is inside a list, that is inside another object? 我可以模拟无法实例化的对象吗? - Can I mock object which is impossible to instantiate? 我可以从字符串开始并实例化该字符串的对象吗? - Can I start with a string and instantiate an object of that string? 我可以在一个查询中获取对象列表以及另一个对象的列表吗? - Can I get list of Object with a list of another object inside in one query? 我想在另一个被破坏的 object 的位置实例化一个 object - I want to instantiate an object at the location of another object which is destroyed 为什么我不能实例化我的类,但我可以在另一个项目中? - Why cant I instantiate my class, but I can in another project? 无法重新实例化/重生我在Unity3D中销毁的对象 - Can't re-instantiate/respawn object I've destroyed in Unity3D 如何在 Unity 中另一个游戏对象的转换 position 实例化 object? - How do I Instantiate an object at the transform position of another GameObject in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM