0
I have the product class and in that class I have this property
public virtual ICollection<CspProduct> CspItems { get; set; }
Here we take the products
var products = _productService.GetAll().ToList();
and here the items
var items = _orderService.GetItemsFromOrder(_order).Where(x => x.CurrencyCode == 23).ToList();
I did the Join below
var result = products.Join(
items,
p => p.Id,
i => i.ProductId,
(p, i) => new { produtos = p, itens = i })
.Where(x => x.produtos.Hidden == false && x.itens.CurrencyCode == 23)
.Select(x => new SubscriptionProductModel
{
Name = x.produtos.Name,
Partnumber = x.produtos.PartNumber,
Markup = 0M,
OfferUri = x.produtos.CspItems.Select(s => new { s.OfferUri}).FirstOrDefault().ToString(),
Dollar = x.itens.BasePriceUSD,
Real = x.itens.BasePriceBRL
}).ToList();
It turns out that when I get on that line inside the Join
OfferUri = x.produtos.CspItems.Select(s => new { s.OfferUri}).FirstOrDefault().ToString()
Makes a mistake of:
Undefined object reference for an object instance
How do I instantiate an object inside another when I’m running a lambda?
Removing Firstordefault() no longer gets the error, but I can’t bring the value of the field, that’s all:
System.Linq.Enumerable+WhereSelectEnumerableIterator
2[CSP.SubscriptionCenter.Core.domain.Microsoftentities.Cspproduct,<>f__AnonymousType321[System.String]]
– pnet
products are coming from a bank consultation? maybe it’s a matter of putting a include to bring your relationship, in your specific case there would be no use instantiating, after all would have object but would not have values
– Lucas Miranda
Yes, products come from a direct Iqueryable. This Include is correct?
var products = _productService.GetAll().Include(x => x.CspItems).ToList();
– pnet