0

I have the following structure of classes:

public partial class OrderOrderData
{
    public OrderOrderDataOfferOrder[] OfferOrder{get;set}
}

public partial class OrderOrderDataOfferOrder
{
    public OrderOrderDataOfferOrderQuestion[] Question{get;set}
}

public partial class OrderOrderDataOfferOrderQuestion
{
    public OrderOrderDataOfferOrderQuestionPrice Price{get;set;}
}

Now I am trying to retrieve that OfferOrder whose Question.Price is not equal to null.

For Example I have Object A, B and C in OfferOrder. Question.Price of C is not equal to null hence my LINQ query should return OfferOrder C to pricedOfferOrder .

I tried to do it by GroupBy but found no luck. I'm trying something like this but still clueless as to how will I get to the Price after grouping by Question

OrderOrderData OrderData = order;
OfferOrder pricedOfferOrder = OrderData.OfferOrder.GroupBy(x=>x.Question)...
Huma Ali
  • 1,759
  • 7
  • 40
  • 66

2 Answers2

1

Simple Where method is sufficient here:

OrderOrderData OrderData = order;

OfferOrder pricedOfferOrder = OrderData.OfferOrder
  .Where(d => d.Question.Any(e => e.Price != null));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
  • I get `object reference not set to an instance of an object` when `Price` is null for all `Question`. How can I avoid this? – Huma Ali Jun 12 '17 at 05:07
  • This can't happen! Are you querying against IQuerable or it is just linq to object? – Jenish Rabadiya Jun 12 '17 at 05:29
  • My bad I'm actually doing this `OfferOrder pricedOfferOrder = OrderData.OfferOrder .Where(d => d.Question.Any(e => e.Price != null)).FirstOrDefault().Question)` – Huma Ali Jun 12 '17 at 05:31
  • Ok so that is the reason if Price is null for all then `OfferOrder pricedOfferOrder = OrderData.OfferOrder .Where(d => d.Question.Any(e => e.Price != null)).FirstOrDefault()` will return nulll and accessing its property Question on null will result in null reference exception. – Jenish Rabadiya Jun 12 '17 at 05:32
  • Any shorthand method, LINQ method to avoid this? – Huma Ali Jun 12 '17 at 05:33
  • People had already given anser to your question that you recently posted here... https://stackoverflow.com/a/44491968/1505865 – Jenish Rabadiya Jun 12 '17 at 05:41
0

There isn't any need to use GroupBy in this case. You could try something like this:

var offerOrder = OrderData.OfferOrder
  .FirstOrDefault(oo => oo.Question.FirstOrDefault(q=>q.Price != null) != null );

Essentially we use the FirstOrDefault method, which takes a predicate and provided that there is at least one item that fulfills this predicate this is the result of the query. Otherwise the default value is returned, which in out case is null. So we are looking for the first OfferOrder if there is any at all, whose Question array contains at least one Question (not null), whose Price is not null.

For further info regarding this method, please have a look here.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Christos
  • 53,228
  • 8
  • 76
  • 108