我在此站点上寻找问题的答案,但找不到任何东西。
我以前的帖子很长,所以我会尝试改进它。
我有这个模型:
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
....
}
我需要从mode加载一个属性,但是此属性是一个虚拟属性,我无法在我的lambda中实例化。我做到了
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;
});
但是当我在注释行中运行上面的代码时,我得到了NullReference的异常。由于Picture我无法实例化它,因此发生了这种情况。我在.Where(First query)
下面做了一个左联接,什么也没做。我尝试以这种方式放置.Include
:_productRepository.Table.Include(pic => pic.Picture).GroupJoin
,什么也没有。我在.Include
之后尝试了.Where
,但没有尝试。换句话说,我做了很多尝试,但没有得到。我没有尝试匿名。我该怎么办?我在做什么错?
EDIT1
如果我这样放置.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
我认为我在qry.ForEach中不需要new Product()
,对吗?那我该怎么办,我需要做一个new Product()
吗?放一个include
我不知道该怎么做或如何做。
答案 0 :(得分:1)
检查是否在Picture
中设置了ForEach
。尽管问题很可能是由于您在对Picture
的查询中未包含Product
实体造成的。尝试在GroupJoin
的条件下将其添加到Picture.Id == Product.PictureId
。
您还可以使用更方便的.Include
到eager load
的{{1}}实体。