1

I need to sort a list of items (IEnumerable ). After sorting with Linq it does not allow me to scan the elements with the foreach.

The code is the following :

const string VERIFICATION_CODE = "where doc.deviceId = \"{0}\" and doc.params =\"\"";

var content = string.Format(VERIFICATION_CODE, dto.DeviceIdorId);

var items = await 
DocumentDBRepository<CosmosDBEvents>.GetItemsAsync(content);

if (items == null || items.Count() == 0)
{
   return null;
}

items = from item in items
        orderby item.descrizione_evento.Severity ascending,item.ts descending
        select item;

MessagesController messageController = new MessagesController(_context);
EventsTypeDescriptionsController eventController = new EventsTypeDescriptionsController(_context);

int codice_evento;
string cultura;

foreach (var item in items)
{
    codice_evento = Convert.ToInt32(item.eventId);
    cultura = GetCulture();
    item.decodifica_evento = messageController.GetMessageWithCulture(codice_evento, cultura);
    ParserDescrizione(item);
    item.descrizione_evento = eventController.GetDetail(codice_evento);
}

return items;

When I try to execute the foreach I get the following error :

"Object reference not set to an instance of an object."

but the collection seems populated if I do a quickwatch.

Can someone help me please.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Simone Spagna
  • 626
  • 7
  • 27
  • 3
    Gah, that looks scary-vulnerable to sql injection attacks. – Joel Coehoorn Aug 02 '19 at 19:37
  • 3
    What do you mean by "it does not allow me to"? Can you post the exact text of the exception your are getting? Are you getting an exception or is it just not working how you expect? – JamesFaix Aug 02 '19 at 19:37

1 Answers1

2

Probably one of the

item.descrizione_evento.Severity

crashes. Use

item.descrizione_evento?.Severity

instead.

Christoph
  • 3,322
  • 2
  • 19
  • 28