sequence contains no elements Error Max() [duplicate]

Your query is not returning any ids. 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;
} 

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)