[英]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. 您也可以使用更方便的.Include
来eager load
Picture
实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.