Icollection property in class is not instantiated

Asked

Viewed 37 times

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+WhereSelectEnumerableIterator2[CSP.SubscriptionCenter.Core.domain.Microsoftentities.Cspproduct,<>f__AnonymousType321[System.String]]

  • 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

  • Yes, products come from a direct Iqueryable. This Include is correct? var products = _productService.GetAll().Include(x => x.CspItems).ToList();

1 answer

1


It may be that the Firstordefault function is returning null because the Cspitems list is empty, or that the variable s of the lambda expression is null. If you want the Offeruri variable to be met even with these conditions use the following code:

OfferUri = x.produtos.CspItems.Select(s => new { s?.OfferUri }).FirstOrDefault()?.ToString()

But I think if your intention is to take the value in s.Offeruri should be like this:

OfferUri = x.produtos.CspItems.FirstOrDefault(s => s?.OfferUri )
  • Weiner Lemes, Um Uri just comes null, so the error as commented. Congratulations! It worked, but I caught it: { OfferUri = 031c9e47-4802-4248-838e-778fb1d2cc05 } and I just need this: 031c9e47-4802-4248-838e-778fb1d2cc05. How do I eliminate keys and Offeruri? It worked in this approach: OfferUri = x.produtos.CspItems.Select(s => new { s?.OfferUri }).FirstOrDefault()?.ToString() the other is making a mistake

  • I decided to put an Offeruri after Firstordefault() and stayed that way: OfferUri = x.produtos.CspItems.Select(s => new { s?.OfferUri }).FirstOrDefault()?.OfferUri.ToString(), but I will consider Weiner Lemes' answer

  • I did it even better. I did it this way and it worked: OfferUri = x.produtos.CspItems.FirstOrDefault()?.OfferUri.ToString()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.