I am Getting:
sequence contains no elements
private int? GetPrecedingSibling(int? contentid,int? templateid) { var value = _sequenceTemplateItemService.Query(e => e.templateId == templateid && e.contentItemId == contentid).Select(t => t.id).Max(); if (value != 0) return value; return null; }
Your query is not returning any id
s. That is why the exception. If your id
type is int?
then use DefaultIfEmpty()
like:
var value = _sequenceTemplateItemService.Query(e => e.templateId == templateid && e.contentItemId == contentid) .Select(t => t.id) .DefaultIfEmpty() .Max();
The other options is to check for Any
records and then return Max
or null.
var tempResult = _sequenceTemplateItemService.Query(e => e.templateId == templateid && e.contentItemId == contentid) .Select(t => t.id); if (tempResult.Any()) { return tempResult.Max(); } else { return null; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With